首页 > js构造函数实例对象传入构造函数,arguments[0].prototype为啥是Object?

js构造函数实例对象传入构造函数,arguments[0].prototype为啥是Object?

想请教下:

function test(){
    console.dir(arguments[0].prototype);
 } 
 var t1 = new test(test); //这里结果为 Object为啥不是test?

假如

function test(){}; 
console.dir(test.prototype); //结果为test 

function test(){
   console.dir(arguments[0].prototype);
 };  
 function fn1(){};  
 
 var t1 = new test(fn1); //结果为object
 
 function test(){console.dir(this);} //结果为object;
 
 function test(){console.dir(this.prototype);} //结果为undefined;
 

假如按new 构造函数 指向这个实例对象的话, 那实例对象没有prototype呀,为啥前两个例子却是Object?

下面这段代码不太好理解


      for(var i = 0; i<arguments.length; i++){
           var o = arguments[i].protoytpe;    
              console.dir(arguments[i].prototype);
             arguments[i].call(arguments[i].prototype);
 
   }  //for循环假如写成  var o = arguments[0].prototype; console.dir(arguments[0].prototype); arguments[0].call(arguments[0].prototype); 就不行了
        function Klass(){
                   function klass(){
                       this.constructor.init.apply(this,arguments);
                              }
                     return klass;
                }
         return Klass();
};
 var A = new Class ();
 A.init = function (){this.x= 300;};
 var B = new Class(A);
for循环假如写成  var o = arguments[0].prototype; console.dir(arguments[0].prototype); arguments[0].call(arguments[0].prototype); 就不行了

服了。?

先说测试不要用alert(),它会把的结果转换成字符串。然后就会发现所有的function输出全是[Object]。

然后再说,第一个测试结果就是test{}对象。每创建一个新的函数就会为其创建一个prototype属性,也就是说test.prototype指向函数的原型对象test{}。工厂函数中this指向的全局widow对象,window对象也没有prototype属性。

至于你为什么输出全是object,因为你用的alert


正是因为对象没有prototype,所以最后一段的第三个才返回undefined
在第一个和第二个中,arguments可以看成对象,但是arguments[0]是传参的函数啊,当然有prototype属性。


请再次测试下你上面代码运行后的输出

1)alert方法期望的输入参数是一个字符串,当输入一个对象的话,那么就会把这个对象转化为字符串表示-调用对象的toString方法,每一个对象都有toString方法,当然你可以覆盖实现自己的
2)console.dir/console.log将调用对象的valueOf方法输出对象的JSON格式的表示
3) prototype属性只有函数对象中拥有,实例对象中是不存在这个属性的
3)任何一个函数对象都有原型属性,对于test函数其默认为

{
    constructor:test
}
Object.prototype.valueOf.call({
    constructor:test
});//输出 test {}
Object.prototype.valueOf.call({
    constructor:Object
});//输出 {}

通过上面的输出我们可以看到,对象的valueOf方法将输出 构造函数 {}这样的形式,构造函数为Object的除外

那么以下的就好理解了

function test(){}; 
console.dir(test.prototype); //结果为test {} 
function test(){
    console.log(arguments[0].prototype);
 } 
 var t1 = new test(test); //输出为 test {} 
function test(){
   console.log(arguments[0].prototype);
 };  
 function fn1(){};  
 var t1 = new test(fn1); //结果为fn1 {}
function test(){console.dir(this);} //this为以test为构造函数的实例对象,结果为test {};
t1 = new test();
function test(){console.dir(this.prototype);} //this为以test为构造函数的实例对象,但是实例对象时没有prototype属性的,故结果为undefined
t1 = new test();

输出是对的,只是你没理解对。

第一段代码,alert输出 Object 是很正常的(其实不太建议用alert输出对象,因为只要是对象如果没有重新定义toString 方法输出的都是[object Object],可以用 console.log 或是console.dir代替)。test是一个函数t1 =new test(test) 是用test作为函数的第一个参数,而argument[0]里面就是第一个参数,所以alert(argument[0].prototype)就是test.prototype 而一个函数的默认的 prototype 就是一个对象(Object),只不过它有一个属性constructor指向test 而已。

第二段代码:你说结果是 test ,是什么意思?

第三段代码: fn1 是函数,所以结果也是object。后面两个函数,alert(this) 这个this就是test的实例对象,alert话输出也是[object Object] 因为 test.prototype 里面没有定义toString 所以alert调用的toString是继承自 Object.prototype


function fn1(){};执行后已经在内存中创建了fn1对象,而所有对象都是继承自Object

【热门文章】
【热门文章】