构造函数
在构造函数内部,this关键字引用的是新创建的对象。
function Car(model, year, miles) { this.model = model; this.year = year; this.miles = miles; this.output= function () { return this.model + "走了" + this.miles + "公里"; }; } var tom= new Car("大叔", 2009, 20000);
有点小问题。首先是使用继承很麻烦了,其次output()在每次创建对象的时候都重新定义了,最好的方法是让所有Car类型的实例都共享这个output()方法,这样如果有大批量的实例的话,就会节约很多内存。
优化:
function Car(model, year, miles) { this.model = model; this.year = year; this.miles = miles; this.output= formatCar; } function formatCar() { return this.model + "走了" + this.miles + "公里"; }
JavaScript里函数有个原型属性叫prototype,当调用构造函数创建对象的时候,所有该构造函数原型的属性在新创建对象上都可用。按照这样,多个Car对象实例可以共享同一个原型
再优化:
function Car(model, year, miles) { this.model = model; this.year = year; this.miles = miles; } /* 注意:这里我们使用了Object.prototype.方法名,而不是Object.prototype 主要是用来避免重写定义原型prototype对象 */ Car.prototype.output= function () { return this.model + "走了" + this.miles + "公里"; }; var tom = new Car("大叔", 2009, 20000);
用其他的方法创建:
function Car(model, year, miles) { this.model = model; this.year = year; this.miles = miles; // 自定义一个output输出内容 this.output = function () { return this.model + "走了" + this.miles + "公里"; } } //方法1:作为函数调用 Car("大叔", 2009, 20000); //添加到window对象上 console.log(window.output()); //方法2:在另外一个对象的作用域内调用 var o = new Object(); Car.call(o, "Dudu", 2010, 5000); console.log(o.output());
该代码的方法1有点特殊,如果不适用new直接调用函数的话,this指向的是全局对象window,我们来验证一下:
//作为函数调用 var tom = Car("大叔", 2009, 20000); console.log(typeof tom); // "undefined" console.log(window.output()); // "大叔走了20000公里"
强制使用new
function Car(model, year, miles) { if (!(this instanceof Car)) { return new Car(model, year, miles); } this.model = model; this.year = year; this.miles = miles; this.output = function () { return this.model + "走了" + this.miles + "公里"; } } var tom = new Car("大叔", 2009, 20000); var dudu = Car("Dudu", 2010, 5000); console.log(typeof tom); // "object" console.log(tom.output()); // "大叔走了20000公里" console.log(typeof dudu); // "object" console.log(dudu.output()); // "Dudu走了5000公里"
推荐,只有在想保留数值状态的时候使用这些包装函数,关于区别可以参考下面的代码:
// 原始string var greet = "Hello there"; // 使用split()方法分割 greet.split(' ')[0]; // "Hello" // 给原始类型添加新属性不会报错 greet.smile = true; // 单没法获取这个值(18章ECMAScript实现里我们讲了为什么) console.log(typeof greet.smile); // "undefined" // 原始string var greet = new String("Hello there"); // 使用split()方法分割 greet.split(' ')[0]; // "Hello" // 给包装函数类型添加新属性不会报错 greet.smile = true; // 可以正常访问新属性 console.log(typeof greet.smile); // "boolean"