比较两个构造函数时:
function C(options, id) {
this.id = id;
// Extend defaults with provided options
this.options = $.extend(true, {}, {
greeting: 'Hello world!',
image: null
}, options);
};
和
function C(params, id) {
this.$ = $(this);
this.id = id;
// Extend defaults with provided options
this.params = $.extend({}, {
taskDescription: '',
solutionLabel: 'Click to see the answer.',
solutionImage: null,
solutionText: ''
}, params);
}
之后是否true
需要该变量$.extends
?
其次,该语句是this.$ = $(this)
必需的,因为第一个构造函数没有该语句,并且它们执行相同的操作。
该true
如果有必要options
有任何嵌套的对象,如果你想使他们的深层副本,而不是指同一嵌套对象为原件的新对象。
简单的例子:
var inner = {
foo: "bar"
};
var outer = {
inner: inner
};
var shallowCopy = $.extend({}, outer);
var deepCopy = $.extend(true, {}, outer);
console.log(shallowCopy.inner.foo); // "bar"
console.log(deepCopy.inner.foo); // "bar"
outer.inner.foo = "updated";
console.log(shallowCopy.inner.foo); // "updated"
console.log(deepCopy.inner.foo); // "bar"
即时复制:
var inner = {
foo: "bar"
};
var outer = {
inner: inner
};
var shallowCopy = $.extend({}, outer);
var deepCopy = $.extend(true, {}, outer);
console.log(shallowCopy.inner.foo); // "bar"
console.log(deepCopy.inner.foo); // "bar"
outer.inner.foo = "updated";
console.log(shallowCopy.inner.foo); // "updated"
console.log(deepCopy.inner.foo); // "bar"
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句