1.2.5.2.函數建構子與「.prototype」
//建立物件 function Person() { this.firstname = 'John'; this.lastname = 'Doe'; } //建立空物件 //呼叫person, 並把this指到person上 var john = new Person(); //印出Person {firstname: "John", lastname: "Doe"} console.log(john) Person.prototype.getFullName = function() { return this.firstname + ' ' + this.lastname }//為函數原型增加一個新方法 //原型中的this會指向物件 Person.prototype.getFullName = function() { return this.firstname + ' ' + this.lastname } console.log(John.getFullName)
Last updated