function test() { return 3; }; var t = new test();//相当于new Object();test()是一个构造函数 alert(t instanceof Number);//false var t = test();//test()是一个方法 alert(t);//3 function test() { return new Number(3); };var t = new test();//相当于new Number(3) alert(t instanceof Number);//true alert(t);//3,因为new Number(3)得到的是一个值再看:function test(o) { return new Number(o); }; var t =new test(3); alert(t instanceof Number);//true alert(new test(3));//3
看下面例子function testFn(){ } var b = new testFn(); function test() { return b; } var a = new test(); alert(a instanceof testFn) //true alert(a === b) //true alert(new Number(3) === 3) //falsenew Number(3)是一个Object,是Number这个类的一个实例,就相当于我例子中的new testFn(),3是一个number 你这两个区别就在于一个返回一个number,一个返回的是一个对象的实例 当你返回的是对象的实例的时候,就有点像是继承了,你那个例子里就是继承了Number类,然后你new test的时候得到的就是Number类的一个实力,值是3.这样alert出来就是3了,事实上,这个new test等同于new Number(3) 且alert((new test()) === 3)//false
When the [[Construct]] property for a Function object F is called, the following steps are taken: 1. Create a new native ECMAScript object. 2. Set the [[Class]] property of Result(1) to "Object". 3. Get the value of the prototype property of the F. 4. If Result(3) is an object, set the [[Prototype]] property of Result(1) to Result(3). 5. If Result(3) is not an object, set the [[Prototype]] property of Result(1) to the original Object prototype object as described in section 15.2.3.1. 6. Invoke the [[Call]] property of F, providing Result(1) as the this value and providing the argument list passed into [[Construct]] as the argument values. 7. If Type(Result(6)) is Object then return Result(6). 8. Return Result(1). new过程 看第7部 如果函数内部是返回的object类型 怎么返回object类型 否则返回新的实例alert(typeof 3) //类型是numberalert(typeof new Number(3)) //类型是object所以return new Number(3)的时候 返回new Number(3) 所以出来的是3而3的时候返回 新的实例(ps 实例是对象 所以是[object Object])
{
return 3;
};
var t = new test();//相当于new Object();test()是一个构造函数
alert(t instanceof Number);//false
var t = test();//test()是一个方法
alert(t);//3
function test()
{
return new Number(3);
};var t = new test();//相当于new Number(3)
alert(t instanceof Number);//true
alert(t);//3,因为new Number(3)得到的是一个值再看:function test(o)
{
return new Number(o);
};
var t =new test(3);
alert(t instanceof Number);//true
alert(new test(3));//3
}
var b = new testFn();
function test()
{
return b;
}
var a = new test();
alert(a instanceof testFn) //true
alert(a === b) //true
alert(new Number(3) === 3) //falsenew Number(3)是一个Object,是Number这个类的一个实例,就相当于我例子中的new testFn(),3是一个number
你这两个区别就在于一个返回一个number,一个返回的是一个对象的实例
当你返回的是对象的实例的时候,就有点像是继承了,你那个例子里就是继承了Number类,然后你new test的时候得到的就是Number类的一个实力,值是3.这样alert出来就是3了,事实上,这个new test等同于new Number(3)
且alert((new test()) === 3)//false
1. Create a new native ECMAScript object.
2. Set the [[Class]] property of Result(1) to "Object".
3. Get the value of the prototype property of the F.
4. If Result(3) is an object, set the [[Prototype]] property of Result(1) to Result(3).
5. If Result(3) is not an object, set the [[Prototype]] property of Result(1) to the original Object prototype object as described in section 15.2.3.1.
6. Invoke the [[Call]] property of F, providing Result(1) as the this value and providing the argument list passed into [[Construct]] as the argument values.
7. If Type(Result(6)) is Object then return Result(6).
8. Return Result(1).
new过程 看第7部 如果函数内部是返回的object类型 怎么返回object类型 否则返回新的实例alert(typeof 3) //类型是numberalert(typeof new Number(3)) //类型是object所以return new Number(3)的时候 返回new Number(3) 所以出来的是3而3的时候返回 新的实例(ps 实例是对象 所以是[object Object])
function test()
{
this.v = 3;
}
test.prototype = {
toString : function(){return this.v}
}alert(new test());alert(new Number(3).toString())
</script>