JavaScript string extension and jQuery.ajax

For the past two days, I have been trying to figure out one problem with my code.

The code is here: http://jsfiddle.net/a7wkpngr/

String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
};



$.ajax({
    url: '/',
    data: $.extend({}, "")
})

If you try to run this code, you can see

TypeError: undefined is not a function // of course, coz this === window

I tried to find "WHY." I found that:

  • The problem is in $.extend(if the second argument is an object, there are no exceptions).
  • If you run $.extendwithout $.ajax, everything will be alright
  • There is NOTHING about the title record in jQuery.

So the question is how and why it String.capitalize()works.

PS I know, I know, it is very difficult to expand the native classes.

+4
source share
3 answers

, , 201 jquery ( 2.0.1).

, ajax. 2. - {} "".

for ( ; i < length; i++ ) {
    // Only deal with non-null/undefined values
    if ( (options = arguments[ i ]) != null ) {
        // Extend the base object
        for ( name in options ) {
            src = target[ name ];
            copy = options[ name ];

loop on property (name in options), String, , ajax. first for loop , 2 , , capize.

:

String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
};

String.prototype.capitalize2 = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
};
String.prototype.trim = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
};
Object.defineProperty(String.prototype, 'test',
{
    value: function(){ return "test" },
    enumerable: false
});

Object.defineProperty(String.prototype, 'test2',
{
    value: function(){ return "test" },
    enumerable: true
});

for (var key in "") {

console.log(key);
}

:


capitalize2
test2

, , .

jsfiddle : http://jsfiddle.net/w9kdp96a/1/

mozilla:

for..in , . .

, capize

Object.defineProperty(String.prototype, 'capitalize',
{
    value: function(){ return this.charAt(0).toUpperCase() + this.slice(1); },
    enumerable: false
});

ajax.

+2

:

  • toUpperCase() - JavaScript. , . .
  • , console.log('hello'.capitalize());
  • $.extend - , :
    $.ajax({
        url: '/',
        data: $.extend({}, {"hello":"world"})
    })

data: {"hello","world"} $.ajax().

$.extend(), , .

+1

To answer your question in the comments: only question is how and why String.capitalize runs,

if you see a jquery source , it will take each data key (foreach (thing in the data)) and try to create a string query with this.

If it is a function, it will call it (comment from jquery source code):

var prefix,
    s = [],
    add = function( key, value ) {
        // If value is a function, invoke it and return its value
        value = jQuery.isFunction( value ) ? value() : ( value == null ? "" : value );
        s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value );
    };

SSA's answer may be a solution, but Object.defineProperty only works in IE9 and above .

+1
source

All Articles