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>
問題:每創建一個對象,對象的方法是新對象,浪費資源
?
2、構造函數方式
<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>
?
問題:
創建對象時比工廠方法更易于理解。
和工廠方法一樣,每個對象都有自己的方法,浪費資源。
?
?
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
?
問題:無法在構造方法中傳遞參數,所有對象共享屬性。
優點:對象共方法,節約資源。
?
4、構造方法+原型方式
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
?
優點:解決了前面提到的問題。
問題:封裝不夠完美。
?
5、動態原形方式
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
?
結論:一種完美的解決方案。
?
6、對象的創建 - JSON
var person = {};
?
var girl = {
name:“miss wang”,
age:20,
show = function(){
? ? ? ?alert("my name is " + this.name);
}
}
?
繼承的實現方式
1、 ?對象冒充
?
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
結論:支持多重繼承,但后面的類可以覆蓋前面類的屬性和方法。繼承后的對象類型和父類對象不匹配
?
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方式以多個參數進行傳值
People.apply(this,[name,age]);//apply方式以數組方式進行傳值
?
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
結論:不支持多重繼承,繼承后的對象類型和父類對象匹配 與50位技術專家面對面20年技術見證,附贈技術全景圖
<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>
問題:每創建一個對象,對象的方法是新對象,浪費資源
?
2、構造函數方式
<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>
?
問題:
創建對象時比工廠方法更易于理解。
和工廠方法一樣,每個對象都有自己的方法,浪費資源。
?
?
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
?
問題:無法在構造方法中傳遞參數,所有對象共享屬性。
優點:對象共方法,節約資源。
?
4、構造方法+原型方式
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
?
優點:解決了前面提到的問題。
問題:封裝不夠完美。
?
5、動態原形方式
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
?
結論:一種完美的解決方案。
?
6、對象的創建 - JSON
var person = {};
?
var girl = {
name:“miss wang”,
age:20,
show = function(){
? ? ? ?alert("my name is " + this.name);
}
}
?
繼承的實現方式
1、 ?對象冒充
?
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
結論:支持多重繼承,但后面的類可以覆蓋前面類的屬性和方法。繼承后的對象類型和父類對象不匹配
?
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方式以多個參數進行傳值
People.apply(this,[name,age]);//apply方式以數組方式進行傳值
?
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
結論:不支持多重繼承,繼承后的對象類型和父類對象匹配 與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
- 上一篇: javascript数组的属性、方法和清
- 下一篇: freemarker常见语法大全,灰常有