jQuery: what is being extended here?
$.ajax($.extend(tru开发者_JAVA技巧e, {...
Can anybody explain what this code is doing?
Yes. From: http://docs.jquery.com/Utilities/jQuery.extend
Extend one object with one or more others, returning the modified object. If no target is specified, the JQuery namespace itself is extended. This can be useful for plugin authors wishing to add new methods to JQuery.
Keep in mind that the target object will be modified, and will be returned from extend().
If a boolean true is specified as the first argument, JQuery performs a deep copy, recursively copying any objects it finds. Otherwise, the copy will share structure with the original object(s). Undefined properties are not copied. However, properties inherited from the object's prototype will be copied over.
The true
means that there is a deep copy being produced between the two objects provided.
What extend does is takes the first object as a base (argument #2), and "extends" it with the second object.
From the jQuery docs:
var settings = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
jQuery.extend(settings, options);
// settings == { validate: true, limit: 5, name: "bar" }
The true
as the first argument means that the extension is being done in a "deep" fashion, so that the old object references are no longer in place.
The subsequent arguments to $.extend()
are being combined into one object. The first argument (true
) is saying do a deep copy.
Extend one object with one or more others, returning the modified object. If no target is specified, the JQuery namespace itself is extended. This can be useful for plugin authors wishing to add new methods to JQuery. Keep in mind that the target object will be modified, and will be returned from extend().
If a boolean true is specified as the first argument, JQuery performs a deep copy, recursively copying any objects it finds. Otherwise, the copy will share structure with the original object(s).
Undefined properties are not copied. However, properties inherited from the object's prototype will be copied over.
精彩评论