這篇文章主要介紹了JavaScript物件導向精要(下部),需要的朋友可以參考下
#建構子與原型物件
##建構子也是函數,用new建立物件時呼叫的函數,與普通函數的一個差異是,其首字母應該要大寫。但如果將建構函式當作普通函式呼叫(缺少new關鍵字),則應該注意this指向的問題。var name = "Pomy"; function Per(){ console.log("Hello "+this.name); } var per1 = new Per(); //"Hello undefined" var per2 = Per(); //"Hello Pomy"
console.log(per1 instanceof Per); //true console.log(per1.constructor === Per); //true
function Person(){} Person.prototype.name="dwqs"; Person.prototype.age=20; Person.prototype.sayName=function() { alert(this.name); }; var per1 = new Person(); per1.sayName(); //dwqs var per2 = new Person(); per2.sayName(); //dwqs alert(per1.sayName == per2.sayName); //true
alert(Person.prototype.isPrototypeOf(per2)); //true per1.blog = "www.ido321.com"; alert(per1.hasOwnProperty("blog")); //true alert(Person.prototype.hasOwnProperty("blog")); //false alert(per1.hasOwnProperty("name")); //false alert(Person.prototype.hasOwnProperty("name")); //true
function Hello(name){ this.name = name; } //重写原型 Hello.prototype = { sayHi:function(){ console.log(this.name); } }; var hi = new Hello("Pomy"); console.log(hi instanceof Hello); //true console.log(hi.constructor === Hello); //false console.log(hi.constructor === Object); //true
#
Hello.prototype = { constructor:Hello, sayHi:function(){ console.log(this.name); } }; console.log(hi.constructor === Hello); //true console.log(hi.constructor === Object); //false
Array.prototype.sum=function(){ return this.reduce(function(prev,cur){ return prev+cur; }); }; var num = [1,2,3,4,5,6]; var res = num.sum(); console.log(res); //21 String.prototype.capit = function(){ return this.charAt(0).toUpperCase()+this.substring(1); }; var msg = "hello world"; console.log(msg.capit()); //"Hello World"
#繼承
利用[[Prototype]]特性,可以實現原型繼承;對於字面量形式的對象,會隱式指定Object.prototype為其[[Prototype]],也可以透過Object.create()顯示指定,其接受兩個參數:第一個是[[Prototype]]指向的物件(原型物件),第二個是可選的屬性描述符物件。var book = { title:"这是书名"; }; //和下面的方式一样 var book = Object.create(Object.prototype,{ title:{ configurable:true, enumerable:true, value:"这是书名", wratable:true } });
var book1 = { title:"JS高级程序设计", getTitle:function(){ console.log(this.title); } }; var book2 = Object.create(book1,{ title:{ configurable:true, enumerable:true, value:"JS权威指南", wratable:true } }); book1.getTitle(); //"JS高级程序设计" book2.getTitle(); //"JS权威指南" console.log(book1.hasOwnProperty("getTitle")); //true console.log(book1.isPrototypeOf("book2")); //false console.log(book2.hasOwnProperty("getTitle")); //false
function Rect(length,width){ this.length = length; this.width = width; } Rect.prototype.getArea = function(){ return this.width * this.length; }; Rect.prototype.toString = function(){ return "[Rect"+this.length+"*"+this.width+"]"; }; function Square(size){ this.length = size; this.width = size; } //修改prototype属性 Square.prototype = new Rect(); Square.prototype.constructor = Square; Square.prototype.toString = function(){ return "[Square"+this.length+"*"+this.width+"]"; }; var rect = new Rect(5,10); var square = new Square(6); console.log(rect.getArea()); //50 console.log(square.getArea()); //36
Square.prototype.toString = function(){ var text = Rect.prototype.toString.call(this); return text.replace("Rect","Square"); }
以上是JavaScript重要知識點總結1的詳細內容。更多資訊請關注PHP中文網其他相關文章!