js 语法 new function与function

来源:互联网 发布:八爪鱼采集器的源码 编辑:程序博客网 时间:2024/06/05 09:37
By the Javascript spec, when a function is invoked with new, Javascript creates a new object, then sets the "constructor" property of that object to the function invoked, and finally assigns that object to the name this. You then have access to the this object for the body of the function.Once the function body is executed, Javascript will return:ANY object if the function manually returns one:function Car(){  this.num_wheels = 4;  return {num_wheels:37};}var car = new Car();alert(car.num_wheels); // 37!The this object if the function has no return statement OR if the function returns a value of a type other than objectfunction Car() {  this.num_wheels = 4;  return 'VROOM';}var car = new Car();alert(car.num_wheels) // 4alert(Car()); // No 'new', so this alerts 'VROOM'
0 0
原创粉丝点击