面向对象编程

JavaScript语言的对象体系,不是基于“类”的,而是基于构造函数(constructor)和原型链(prototype)

构造函数名字的第一个字母通常大写

1.构造函数基本格式(首字母大写):

var Vehicle = function () {
  this.price = 1000;//this指向实例
};

2.如果return语句返回的是一个跟this无关的新对象,new命令会返回这个新对象,而不是this对象。

var Vehicle = function (){
  this.price = 1000;
  return { price: 2000 };
};

(new Vehicle()).price//造函数Vehiclereturn语句,返回的是一个新对象。new命令会返回这个对象,而不是this对象
// 2000

new.target指向当前函数,否则为undefined

    function f() {
  console.log(new.target === f);//new.target指向当前函数
}

f() // false
new f() // true

 

posted on 2017-06-13 20:57  我爱吃豌豆  阅读(139)  评论(0编辑  收藏  举报

导航