Home  >  Q&A  >  body text

javascript - Is the _extend method used to traverse object properties? How to understand the two parameters?

Can anyone explain:
Why does _extend appear twice? What do they mean? Does it mean the default method is called? ?
The first time is Base.prototype._extend.
Especially the second time, what do the destination and source represent?

 _extend = function self(destination, source) {
     ......
 }

The following is the part of the code that caused my confusion:

define(function(){
var Base = function(){};
Base.prototype._extend = function(dest, src){
    var _complete,
    _extend,
    _isObject;
    _isObject = function(o){
        return (Object.prototype.toString.call(o) === '[object Object]' || Object.prototype.toString.call(o) === '[object Array]');
    },
    _extend = function self(destination, source) {
        var property;
        for (property in destination) {
            if (destination.hasOwnProperty(property)) {
                if (_isObject(destination[property]) && _isObject(source[property])) {
                    self(destination[property], source[property]);
                };

                if (source.hasOwnProperty(property)) {
                    continue;
                } else {
                    source[property] = destination[property];
                }
            }
        }
    }
    //省略部分代码...
    }})
世界只因有你世界只因有你2732 days ago471

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-05-18 11:03:02

    _extend The first one is variable declaration, and the second one is variable assignment.

    The function is to traverse the destination variable and copy its internal attributes to the source. If the attribute already exists on the source, it will not be copied.

    reply
    0
  • Cancelreply