jquery教程

推荐列表 站点导航

当前位置:首页 > jquery > jquery教程 >

JavaScript 常见对象类创建代码与各自的优缺点分析

来源:网络整理  作者:  发布时间:2020-12-20 02:44
本篇文章介绍了,javascript中常见对象的创建方法与代码,并就其各自的优缺点进行分析。有需要的朋友,可以参考下...

在javascript中的类定义方式,常用的是杂合prototype/constructor 和 动态prototype方式。

构建一个类有好几种方法:
1、Factory 方式
 

function createCar(){
var car = new Object();
car.color=”b”;
car.length=1;
car.run=function(){alert(”run”);}
return car;
}

定义这么一个函数之后,就可以用:
 

var car1 = createCar();
var car2 = createCar();
 

来创建新的对象,这种方式的问题是每一次创建一个car对象,run Function也都必须重新创建一次,浪费内存。

2、Constructor方式
 

function Car(){
this.color=”b”;
this.length=1;
this.run=function(){alert(”run”);}
}
var car1=new Car();
var car2=new Car();
 

这是最基本的方式,但是也存在和factory方式一样的毛病。

3、prototype方式
 

function Car(){
}
Car.prototype.color=”b”;
Car.prototype.length=1;
Car.prototype.run=function(){alert(”run”);
}
 

此方式的缺点:当这个类有一个引用属性时,改变一个对象的这个属性也会改变其他对象得属性。
例如:
 

Car.prototype.data1=new Array();
var car1=new Car();
var car2=new Car();
car1.data1.push(”a”);
 

此时,car2.data也就包含了”a”元素。

4、Prototype/Constructor杂合方式 也是最常用的方式
 

function Car(){
this.color=”b”;
this.length=1;
this.data1=new Array();
}
Car.prototype.run=function(){
alert(”dddd”);
}
 

摒弃了以上其它方式的缺点,是目前比较大范围使用的方式。

5、动态prototype方式 也很常用
 

function Car(){
this.color=”b”;
this.length=1;
this.data1=new Array();

if(typeof Car.initilize==”undefined”){
Car.prototype.run=function(){alert(”a”);}
}

Car.initilize=true;
}

总结:
以上几种方式中,最常用的是杂合prototype/constructor 和 动态prototype方式。
希望以上内容,有助于大家理解javascript对象类的创建方法,jquery中文网,祝大家学习进步。

相关热词: javascript

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jq/jc/5923.shtml

最新文章
PHP识别相片是否是颠倒的 PHP识别相片是否是颠倒的

时间:2020-12-28

python编程有哪些ide python编程有哪些ide

时间:2020-12-28

python开发工程师是做什么 python开发工程师是做什么

时间:2020-12-28

php构造函数的作用 php构造函数的作用

时间:2020-12-28

php怎么跟数据库连接 php怎么跟数据库连接

时间:2020-12-28

php实现顺序线性表 php实现顺序线性表

时间:2020-12-28

Python多重继承中的菱形继 Python多重继承中的菱形继

时间:2020-12-28

php中break的作用 php中break的作用

时间:2020-12-28

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

JavaScript 常见对象类创建代码与各自的优缺点分析

2020-12-20 编辑:

在javascript中的类定义方式,常用的是杂合prototype/constructor 和 动态prototype方式。

构建一个类有好几种方法:
1、Factory 方式
 

function createCar(){
var car = new Object();
car.color=”b”;
car.length=1;
car.run=function(){alert(”run”);}
return car;
}

定义这么一个函数之后,就可以用:
 

var car1 = createCar();
var car2 = createCar();
 

来创建新的对象,这种方式的问题是每一次创建一个car对象,run Function也都必须重新创建一次,浪费内存。

2、Constructor方式
 

function Car(){
this.color=”b”;
this.length=1;
this.run=function(){alert(”run”);}
}
var car1=new Car();
var car2=new Car();
 

这是最基本的方式,但是也存在和factory方式一样的毛病。

3、prototype方式
 

function Car(){
}
Car.prototype.color=”b”;
Car.prototype.length=1;
Car.prototype.run=function(){alert(”run”);
}
 

此方式的缺点:当这个类有一个引用属性时,改变一个对象的这个属性也会改变其他对象得属性。
例如:
 

Car.prototype.data1=new Array();
var car1=new Car();
var car2=new Car();
car1.data1.push(”a”);
 

此时,car2.data也就包含了”a”元素。

4、Prototype/Constructor杂合方式 也是最常用的方式
 

function Car(){
this.color=”b”;
this.length=1;
this.data1=new Array();
}
Car.prototype.run=function(){
alert(”dddd”);
}
 

摒弃了以上其它方式的缺点,是目前比较大范围使用的方式。

5、动态prototype方式 也很常用
 

function Car(){
this.color=”b”;
this.length=1;
this.data1=new Array();

if(typeof Car.initilize==”undefined”){
Car.prototype.run=function(){alert(”a”);}
}

Car.initilize=true;
}

总结:
以上几种方式中,最常用的是杂合prototype/constructor 和 动态prototype方式。
希望以上内容,有助于大家理解javascript对象类的创建方法,jquery中文网,祝大家学习进步。

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jq/jc/5923.shtml

相关文章

风云图片

推荐阅读

返回jquery教程频道首页