I am trying to use the extend for the defaults,options in a plugin. But it does not work as described in documentation.
我试图使用扩展为插件中的默认值,选项。但它不像文档中描述的那样工作。
var empty = {}
var defaults = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
var settings = $.extend(empty, defaults, options);
Here the settings is supposed to Merge defaults and options, without modifying the defaults.
这里的设置应该是Merge默认值和选项,而不修改默认值。
but a alert(settings.validate) shows true..which means it has overridden...any comments
但警报(settings.validate)显示true ..这意味着它已被覆盖...任何评论
THnks Coool
3 个解决方案
#1
Your code:
var empty = {}
var defaults = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
var settings = $.extend(empty, defaults, options);
... results in ...
... 结果是 ...
settings == { validate: true, limit: 5, name: "bar" }
empty == { validate: true, limit: 5, name: "bar" }
which is exactly what the documentation says it should do:
这正是文档说它应该做的:
jQuery.extend( [deep], target, object1, [objectN] )
jQuery.extend([deep],target,object1,[objectN])
Extend one object with one or more others, returning the modified object.
使用一个或多个其他对象扩展一个对象,返回修改后的对象。
It modifies the variable empty
and returns a reference to that same variable, which you store in settings
.
它修改变量empty并返回对存储在设置中的同一变量的引用。
#2
There is something wrong though if you take it a little further :
如果再深入一点,那就有问题了:
var empty = {};
var defaults = { validate: false, limit: 5, name: "foo", buttons : {}};
var options = { validate: true, name: "bar"};
var settings = $.extend(empty, defaults, options);
settings.buttons['Ok'] =
function()
{
alert( "TEST");
};
After this code runs defaults.buttons contains ["Ok"] can anyone explain why?
在此代码运行后,defaults.buttons包含[“Ok”]任何人都可以解释原因吗?
#3
Hmmm... and if you write var settings = $.extend(empty, options, defaults);
? I think you will have setting with full set of options+defaults props. Where dafaults will overwrite options with same name. Is it what you want?
嗯...如果你写var settings = $ .extend(空,选项,默认值);?我想你会设置全套选项+默认道具。 dafaults将覆盖具有相同名称的选项。这是你想要的吗?
BTW defaults means defaults. This way you can stop making mandatory params. And thats why setting have to have defaults overwitten with settings. Isn't it? And thats why you sample is correct and mine is strange ;) But answers your question (i hope).
BTW默认值表示默认值。这样你就可以停止制作强制性参数了。这就是为什么设置必须通过设置覆盖默认值。不是吗?这就是为什么你的样本是正确的,我的是奇怪的;)但回答你的问题(我希望)。
#1
Your code:
var empty = {}
var defaults = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
var settings = $.extend(empty, defaults, options);
... results in ...
... 结果是 ...
settings == { validate: true, limit: 5, name: "bar" }
empty == { validate: true, limit: 5, name: "bar" }
which is exactly what the documentation says it should do:
这正是文档说它应该做的:
jQuery.extend( [deep], target, object1, [objectN] )
jQuery.extend([deep],target,object1,[objectN])
Extend one object with one or more others, returning the modified object.
使用一个或多个其他对象扩展一个对象,返回修改后的对象。
It modifies the variable empty
and returns a reference to that same variable, which you store in settings
.
它修改变量empty并返回对存储在设置中的同一变量的引用。
#2
There is something wrong though if you take it a little further :
如果再深入一点,那就有问题了:
var empty = {};
var defaults = { validate: false, limit: 5, name: "foo", buttons : {}};
var options = { validate: true, name: "bar"};
var settings = $.extend(empty, defaults, options);
settings.buttons['Ok'] =
function()
{
alert( "TEST");
};
After this code runs defaults.buttons contains ["Ok"] can anyone explain why?
在此代码运行后,defaults.buttons包含[“Ok”]任何人都可以解释原因吗?
#3
Hmmm... and if you write var settings = $.extend(empty, options, defaults);
? I think you will have setting with full set of options+defaults props. Where dafaults will overwrite options with same name. Is it what you want?
嗯...如果你写var settings = $ .extend(空,选项,默认值);?我想你会设置全套选项+默认道具。 dafaults将覆盖具有相同名称的选项。这是你想要的吗?
BTW defaults means defaults. This way you can stop making mandatory params. And thats why setting have to have defaults overwitten with settings. Isn't it? And thats why you sample is correct and mine is strange ;) But answers your question (i hope).
BTW默认值表示默认值。这样你就可以停止制作强制性参数了。这就是为什么设置必须通过设置覆盖默认值。不是吗?这就是为什么你的样本是正确的,我的是奇怪的;)但回答你的问题(我希望)。