关于 new 操作符的原理,有下面的经典代码,我不理解的是,source.apply 里的source 难道不包括其它参数,只包括函数?
var a = function(sA,sH){
    var x = "x";
    this.a = sA;
    this.h = sH;
    this.say = function(){alert(this.a+','+x)}
}
a.prototype.hi = function(){alert(this.h)}var createInstance = function(source){
    var p = {}
    var args = Array.prototype.slice.call(arguments,1);//arguments 包括 a 及后面所说的 args 吧?    p.__proto__ = source.prototype;
    source.apply(p,args);//这里的source 难道不包括 args ,只有 a ?    return p;
}var A = createInstance(a,"A","hi A");
A.say();

解决方案 »

  1.   

    楼主需要的是理解call和apply两个方法.
    你红色标识部分跟new没什么联系.
      

  2.   

    apply MethodSee Also
    Applies To: Function Object
    Requirements
    Version 5.5
    Applies a method of an object, substituting another object for the current object.apply([thisObj[,argArray]])
    Arguments
    thisObj 
    Optional. The object to be used as the current object. 
    argArray 
    Optional. Array of arguments to be passed to the function. 
    Res
    If argArray is not a valid array or is not the arguments object, then a TypeError results.If neither argArray nor thisObj are supplied, the global object is used as thisObj and is passed no arguments.
      

  3.   

    call MethodSee Also
    Applies To: Function Object
    Requirements
    Version 5.5
    Calls a method of an object, substituting another object for the current object.call([thisObj[, arg1[, arg2[,  [, argN]]]]])
    Arguments
    thisObj 
    Optional. The object to be used as the current object. 
    arg1, arg2, , argN 
    Optional. List of arguments to be passed to the method. 
    Res
    The call method is used to call a method on behalf of another object. The call method allows you to change the object context of a function from the original context to the new object specified by thisObj.If thisObj is not supplied, the global object is used as thisObj.
      

  4.   

    arguments是全部参数
    Array.prototype.slice.call(arguments,1);取到从第1个参数后面的余下参数,不包括第一个参数,返回为一数组。
    source是第一个参数
      

  5.   

    var a = function(sA,sH){ 
        var x = "x"; 
        this.a = sA; 
        this.h = sH; 
        this.say = function(){alert(this.a+','+x)} 

    a.prototype.hi = function(){alert(this.h)} var createInstance = function(source){ 
        var p = {} 
        var args = Array.prototype.slice.call(arguments,1);//arguments 包括a,"A","hi A"
        p.__proto__ = source.prototype; 
        source.apply(p,args);//这里的source 只有 a 。
       return p; 
    } var A = createInstance(a,"A","hi A"); 
    A.say();
      

  6.   

    你说的我明白,但我还是有点不明白:为什么createInstance的形式参数 source 不是包括 3 个参数:a,"A","hi A" ?
      

  7.   

    楼主要理解两个地方:var createInstance = function(source){ 
    ...
    var args = Array.prototype.slice.call(arguments,1);//1
    source.apply(p,args);//2
    ...
    }1.取出所有除形参source外的其他所有参数;
    2.理解apply函数功能;PS,请在使用SpiderMonkey引擎的浏览器中测试此代码。
      

  8.   

    服了,就是这么规定的。
    Although a JavaScript function is defined with a fixed number of named arguments, it can be passed any number of arguments when it is invoked. The arguments[] array allows full access to these argument values, even when some are unnamed. Suppose you define a function f that expects to be passed one argument, x. If you invoke this function with two arguments, the first argument is accessible within the function by the parameter name x or as arguments[0]. The second argument is accessible only as arguments[1].