js自定义对象和类
1.工廠方式
<script type="text/javascript">
function createObject(name){
? ? ? ?var p = new Object();
? ? ? ?p.name=name;
? ? ? ?p.say = function(){alert(p.name+'ff');}
? ? ? ?return p;
}
var p1 = createObject("p1");
var p2 = createObject("p2");
alert(p1.name+" "+p2.name);
p1.say();p2.say();
alert(p1.say==p2.say); //false
</script>
問(wèn)題:每創(chuàng)建一個(gè)對(duì)象,對(duì)象的方法是新對(duì)象,浪費(fèi)資源
?
2、構(gòu)造函數(shù)方式
<script type="text/javascript">
function Person(name){
this.name = name;
this.say = function(){
alert("I am "+this.name);
}
}
var p1 = new Person("wang");
var p2 = new Person("li");
p1.say();
p2.say();
alert(p1.say==p2.say); //false
?
</script>
?
問(wèn)題:
創(chuàng)建對(duì)象時(shí)比工廠方法更易于理解。
和工廠方法一樣,每個(gè)對(duì)象都有自己的方法,浪費(fèi)資源。
?
?
3、原型方式
function Person(){}
Person.prototype.name = "";
Person.prototype.say = function(){
alert("I am "+this.name);
}
?
var p1 = new Person();
var p2 = new Person();
alert(p1.say == p2.say);//true
?
問(wèn)題:無(wú)法在構(gòu)造方法中傳遞參數(shù),所有對(duì)象共享屬性。
優(yōu)點(diǎn):對(duì)象共方法,節(jié)約資源。
?
4、構(gòu)造方法+原型方式
function Person(name){
? ? ? ?this.name = name;
}
Person.prototype.say = function(){
? ? ? ?alert("I am "+this.name);
}
var p1 = new Person("wang");
var p2 = new Person("li");
p1.say();
p2.say();
alert(p1.say==p2.say); //true
?
優(yōu)點(diǎn):解決了前面提到的問(wèn)題。
問(wèn)題:封裝不夠完美。
?
5、動(dòng)態(tài)原形方式
function Person(name){
this.name = name;
if(Person.prototype.say == undefined){
? ? ? ?Person.prototype.say = function(){
? ? ? ? ? ? ? alert("I am "+this.name);
? ? ? ? ? ? ? }
? ? ? ?}
}
var p1 = new Person("wang");
var p2 = new Person("li");
p1.say();
p2.say();
alert(p1.say==p2.say); //true
?
結(jié)論:一種完美的解決方案。
?
6、對(duì)象的創(chuàng)建 - JSON
var person = {};
?
var girl = {
name:“miss wang”,
age:20,
show = function(){
? ? ? ?alert("my name is " + this.name);
}
}
?
繼承的實(shí)現(xiàn)方式
1、 ?對(duì)象冒充
?
function People(name){
this.name = name;
this.say = function(){
alert("I am "+this.name);
? ? ? ?}
}
function WhitePeople(name){
this.inherit = People;
this.inherit(name);
delete this.inherit;
?
this.color = function(){
? ? ? ? ? ? ? alert("I am white people.");
}
}
var p = new WhitePeople("wang");
p.say();
p.color();
alert(p instanceof People); //false
結(jié)論:支持多重繼承,但后面的類可以覆蓋前面類的屬性和方法。繼承后的對(duì)象類型和父類對(duì)象不匹配
?
2、利用call()和apply()冒充
function People(name,age){
this.name = name;
this.age = age;
this.say = function(){
alert("I am "+this.name+" ?"+this.age);
? ? ? ?}
}
function WhitePeople(name,age){
//People.call(this,name,age);//call方式以多個(gè)參數(shù)進(jìn)行傳值
People.apply(this,[name,age]);//apply方式以數(shù)組方式進(jìn)行傳值
?
this.color = function(){
? ? ? ? ? ? ? alert("I am white people.");
}
}
var p = new WhitePeople("wang",34);
p.say();
?
p.color();
?
alert(p instanceof People);
?
?
3、原型鏈繼承
//父類
function People(name){
? ? ? ?this.name = name;
}
People.prototype.say = function(){
? ? ? ?alert("I am "+this.name);
}
?
//子類
function ChinaPeople(name,area){
? ? ? ?People.call(this,name);
? ? ? ?this.area = area;
}
ChinaPeople.prototype = new People();
ChinaPeople.prototype.from = function(){
? ? ? ?alert("I'am from "+this.area);
}
?
var p = new ChinaPeople("wang","si chuan");
p.say();
p.from();
alert(p instanceof People); //true
結(jié)論:不支持多重繼承,繼承后的對(duì)象類型和父類對(duì)象匹配 與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖
<script type="text/javascript">
function createObject(name){
? ? ? ?var p = new Object();
? ? ? ?p.name=name;
? ? ? ?p.say = function(){alert(p.name+'ff');}
? ? ? ?return p;
}
var p1 = createObject("p1");
var p2 = createObject("p2");
alert(p1.name+" "+p2.name);
p1.say();p2.say();
alert(p1.say==p2.say); //false
</script>
問(wèn)題:每創(chuàng)建一個(gè)對(duì)象,對(duì)象的方法是新對(duì)象,浪費(fèi)資源
?
2、構(gòu)造函數(shù)方式
<script type="text/javascript">
function Person(name){
this.name = name;
this.say = function(){
alert("I am "+this.name);
}
}
var p1 = new Person("wang");
var p2 = new Person("li");
p1.say();
p2.say();
alert(p1.say==p2.say); //false
?
</script>
?
問(wèn)題:
創(chuàng)建對(duì)象時(shí)比工廠方法更易于理解。
和工廠方法一樣,每個(gè)對(duì)象都有自己的方法,浪費(fèi)資源。
?
?
3、原型方式
function Person(){}
Person.prototype.name = "";
Person.prototype.say = function(){
alert("I am "+this.name);
}
?
var p1 = new Person();
var p2 = new Person();
alert(p1.say == p2.say);//true
?
問(wèn)題:無(wú)法在構(gòu)造方法中傳遞參數(shù),所有對(duì)象共享屬性。
優(yōu)點(diǎn):對(duì)象共方法,節(jié)約資源。
?
4、構(gòu)造方法+原型方式
function Person(name){
? ? ? ?this.name = name;
}
Person.prototype.say = function(){
? ? ? ?alert("I am "+this.name);
}
var p1 = new Person("wang");
var p2 = new Person("li");
p1.say();
p2.say();
alert(p1.say==p2.say); //true
?
優(yōu)點(diǎn):解決了前面提到的問(wèn)題。
問(wèn)題:封裝不夠完美。
?
5、動(dòng)態(tài)原形方式
function Person(name){
this.name = name;
if(Person.prototype.say == undefined){
? ? ? ?Person.prototype.say = function(){
? ? ? ? ? ? ? alert("I am "+this.name);
? ? ? ? ? ? ? }
? ? ? ?}
}
var p1 = new Person("wang");
var p2 = new Person("li");
p1.say();
p2.say();
alert(p1.say==p2.say); //true
?
結(jié)論:一種完美的解決方案。
?
6、對(duì)象的創(chuàng)建 - JSON
var person = {};
?
var girl = {
name:“miss wang”,
age:20,
show = function(){
? ? ? ?alert("my name is " + this.name);
}
}
?
繼承的實(shí)現(xiàn)方式
1、 ?對(duì)象冒充
?
function People(name){
this.name = name;
this.say = function(){
alert("I am "+this.name);
? ? ? ?}
}
function WhitePeople(name){
this.inherit = People;
this.inherit(name);
delete this.inherit;
?
this.color = function(){
? ? ? ? ? ? ? alert("I am white people.");
}
}
var p = new WhitePeople("wang");
p.say();
p.color();
alert(p instanceof People); //false
結(jié)論:支持多重繼承,但后面的類可以覆蓋前面類的屬性和方法。繼承后的對(duì)象類型和父類對(duì)象不匹配
?
2、利用call()和apply()冒充
function People(name,age){
this.name = name;
this.age = age;
this.say = function(){
alert("I am "+this.name+" ?"+this.age);
? ? ? ?}
}
function WhitePeople(name,age){
//People.call(this,name,age);//call方式以多個(gè)參數(shù)進(jìn)行傳值
People.apply(this,[name,age]);//apply方式以數(shù)組方式進(jìn)行傳值
?
this.color = function(){
? ? ? ? ? ? ? alert("I am white people.");
}
}
var p = new WhitePeople("wang",34);
p.say();
?
p.color();
?
alert(p instanceof People);
?
?
3、原型鏈繼承
//父類
function People(name){
? ? ? ?this.name = name;
}
People.prototype.say = function(){
? ? ? ?alert("I am "+this.name);
}
?
//子類
function ChinaPeople(name,area){
? ? ? ?People.call(this,name);
? ? ? ?this.area = area;
}
ChinaPeople.prototype = new People();
ChinaPeople.prototype.from = function(){
? ? ? ?alert("I'am from "+this.area);
}
?
var p = new ChinaPeople("wang","si chuan");
p.say();
p.from();
alert(p instanceof People); //true
結(jié)論:不支持多重繼承,繼承后的對(duì)象類型和父類對(duì)象匹配 與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖
總結(jié)
- 上一篇: javascript数组的属性、方法和清
- 下一篇: freemarker常见语法大全,灰常有