最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

In $.extend(true, {}, obj) in javascript, what is the purpose of 'true'? - Stack Overflow

programmeradmin9浏览0评论

When paring the two constructors:

  function C(options, id) {
    this.id = id;

    // Extend defaults with provided options
    this.options = $.extend(true, {}, {
      greeting: 'Hello world!',
      image: null
    }, options);


  };

and

  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);
  }

Is the true variable necessary after $.extends?

Secondly, is the statement this.$ = $(this) necessary as the first constructor does not have it and they do the same thing.

When paring the two constructors:

  function C(options, id) {
    this.id = id;

    // Extend defaults with provided options
    this.options = $.extend(true, {}, {
      greeting: 'Hello world!',
      image: null
    }, options);


  };

and

  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);
  }

Is the true variable necessary after $.extends?

Secondly, is the statement this.$ = $(this) necessary as the first constructor does not have it and they do the same thing.

Share Improve this question edited Mar 20, 2016 at 16:44 timothyylim asked Mar 20, 2016 at 16:24 timothyylimtimothyylim 1,5372 gold badges17 silver badges33 bronze badges 1
  • 2 It's necessary when you want the behavior described in the documentation that true provides. And this.$ = $(this); is necessary if you want that assignment to take place. None of these things are absolutely required. You need to decide what is needed for your code in each circumstance. – user1106925 Commented Mar 20, 2016 at 16:26
Add a ment  | 

1 Answer 1

Reset to default 9

The true is necessary if options has any nested objects, if you want to make a deep copy of them rather than having the new objects referring to the same nested objects as the originals.

Simple example:

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"

Live Copy:

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./ajax/libs/jquery/1.11.1/jquery.min.js"></script>

More in the $.extend documentation.

发布评论

评论列表(0)

  1. 暂无评论