function Person(name, age, job) { this.name = name; this.age = age; this.job = job; this.sayName = function() { return this.name } } // 測試 var person = new Person('Nicholas', 29, 'Engineer'); // 檢測對象類型 console.log(person.constructor === Person); // true console.log(person instanceof Person); // true console.log(Person instanceof Object); // true
new操做符
建立實例。instanceof操做符
檢測對象類型。var person1 = new Person('Nicholas', 29, 'Engineer'); var person2 = new Person('Greg', 30, 'Doctor'); console.log(person1.sayName === person2.sayName); // false
function Person() {} Person.prototype.name = "Nicholas"; Person.prototype.age = 29; Person.prototype.job = "Engineer"; Person.prototype.sayName = function() { return this.name; } // 測試 var person = new Person(); console.log(person.sayName()); // Nicholas // 檢測對象類型 console.log(person.constructor === Person); // true console.log(person instanceof Person); // true console.log(Person instanceof Object); // true
prototype
屬性,指向函數的原型。constructor
屬性,指向構造函數。[[Prototype]]
或_proto_
,指向構造函數的原型。isPrototypeOf()
方法會返回一個布爾值,能夠肯定實例和原型的關係。Object.getPrototypeOf()
方法會返回實例對應的原型。// isPrototypeOf()方法 console.log(Person.prototype.isPrototypeOf(person)); // true // Object.getPrototypeOf()方法 console.log(Object.getPrototypeOf(person) === Person.prototype); // true
// 添加屬性 person.name = "Greg"; console.log(person.name); // Greg // 刪除屬性 delete person.name; console.log(person.name); // Nicholas
Object.defineProperty()
方法定義。function Person() {}; Person.prototype = { // constructor: Person, name: 'Nicholas', age: 29, job: 'Engineer', sayName: function() { return this.name; } }; Object.defineProperty(Person.prototype, 'constructor', { enumerable: false, value: Person }); // 測試 var person = new Person(); console.log(person.sayName()); // Nicholas // 檢測對象類型 console.log(person.constructor === Person); // true console.log(person instanceof Person); // true console.log(Person instanceof Object); // true
new操做符
建立實例。instanceof操做符
檢測對象類型。function Student() {} Student.prototype.friends = ['Shelby', 'Court']; // 測試 var stu1 = new Student(); var stu2 = new Student(); stu1.friends.push('Van'); // 實例stu1修改了原型的friends屬性,並影響到了stu2 console.log(stu1.friends); // ["Shelby", "Court", "Van"] console.log(stu2.friends); // ["Shelby", "Court", "Van"]
function Person(name, age, job) { this.name = name; this.age = age; this.job = job; } Person.prototype.sayName = function() { return this.name; } // 測試 var person = new Person('Nicholas', 29, 'Engineer'); console.log(person.sayName()); // Nicholas // 檢測對象類型 console.log(person.constructor === Person); // true console.log(person instanceof Person); // true console.log(Person instanceof Object); // true
new操做符
建立實例。instanceof操做符
檢測對象類型。function Person(name, age, job) { // 屬性 this.name = name; this.age = age; this.job = job; // 方法 if (typeof this.sayName !== 'function') { Person.prototype.sayName = function() { return this.name; } } } // 測試 var person = new Person('Nicholas', 29, 'Engineer'); console.log(person.sayName()); // Nicholas // 檢測對象類型 console.log(person.constructor === Person); // true console.log(person instanceof Person); // true console.log(Person instanceof Object); // true
new操做符
建立實例。instanceof操做符
檢測對象類型。function Person(name, age, job) { var o = new Object(); o.name = name; o.age = age; o.job = job o.sayName = function() { return o.name; } return o; } // 測試 var person = new Person('Nicholas', 29, 'Engineer'); console.log(person.sayName()); // Nicholas // 檢測對象類型 console.log(person.constructor === Person); // false console.log(person instanceof Person); // false console.log(Person instanceof Object); // true
new操做符
建立實例。在不使用new操做符時,又被稱爲工廠模式。instanceof操做符
檢測對象類型。由於返回的對象與構造函數及原型之間沒有關係。參考:《JavaScript高級程序設計》javascript