想请教下:
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); 就不行了
请再次测试下你上面代码运行后的输出
1)alert方法期望的输入参数是一个字符串,当输入一个对象的话,那么就会把这个对象转化为字符串表示-调用对象的toString方法,每一个对象都有toString方法,当然你可以覆盖实现自己的
2)console.dir/console.log将调用对象的valueOf方法输出对象的JSON格式的表示
3) prototype属性只有函数对象中拥有,实例对象中是不存在这个属性的
3)任何一个函数对象都有原型属性,对于test函数其默认为
通过上面的输出我们可以看到,对象的valueOf方法将输出
构造函数 {}这样的形式,构造函数为Object的除外
那么以下的就好理解了