Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Several common methods of creating objects by javascript

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

本篇内容介绍了"javascript创建对象的几种常见方法"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

既然是面向对象,首先我们要知道如何创建一个对象,以下列出了创建对象的几种常见方法:

A.直接创建一个对象实例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

//直接实例化一个对象

var Person1 = { Name: "梦在旅途", Age: 29, Sex: "男", Height: 178 };

alert(Person1.Name);

var Person2 = new Object();

Person2.Name = "梦在旅途";

Person2.Age = 29;

Person2.Sex = "男";

Person2.Height = 178;

alert(Person2.Name);

//这个是上面的简写

var Person3 = new Object({ Name: "梦在旅途", Age: 29, Sex: "男", Height: 178 });

alert(Person3.Name);

优点:直接创建一个对象,无需提前定义类型;

缺点:无法实现复用;

B.先定义后实例化对象:

1

2

3

4

5

6

7

8

9

10

//先定义类,再实例化成对象

function Person4(n,a,s,h) {

this.Name = n;

this.Age = a;

this.Sex = s;

this.Height = h;

}

var p4 = new Person4("梦在旅途", 29, "男", 178);

alert(p4.Age);

优点:类似面向对象编程语言的构造函数,容易理解,且定义后可通过new关键字实例化多个对象,实现复用。

缺点:需先定义后才能实例化;

综上所述,建议采用B方法来创建对象。

实现封装,即只暴露公共方法与公共属性,隐藏实现细节(私有方法、属性)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

function Person5(n, a, s, h) {

//公共属性

this.Name = n;

this.Age = a;

this.Sex = s;

this.Height = h;

//公共方法

this.AfterYear = function (count) {

updateAge(count);

alert(_currentYear +"后,我已经:" + this.Age +"岁了!");

};

this.Say = function () {

alert("我的个人信息--> Name: "+ this.Name+", Age: "+ this.Age +", Sex: "+ this.Sex +", Height:" + this.Height);

}

//私有属性与方法

var _self = this;

var _currentYear = 2015;

function updateAge(count) {

_currentYear += count;

_self.Age += count;

};

}

var p5 = new Person5("梦在旅途", 29, "男", 178);

p5.AfterYear(10);

p5.AfterYear(25);

利用原型链实现继承,即一个对象包含另一个对象的所有公共属性与方法,实现继承的方法有很多,我觉得采用如下形式来模拟继承更符合面向对象的思维:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

function SoftEngineer(n, a, s, h, lang) {

Person5.call(this, n, a, s, h);//将Person5的所有属性与方法包含到SoftEngineer中,从而实现继承

this.Lang = lang;

this.SayCode = function () {

alert("我是一名软件工程师,我会" + this.Lang + "编程语言!");

}

this.Working = function () { };//空方法,类似面向对象中的虚方法

}

SoftEngineer.prototype = new Person5(); //将SoftEngineer的原型指定Person5的实例

var softengr = new SoftEngineer("梦在旅途", 29, "男", 178, "javascript");

softengr.Say();

softengr.SayCode();

利用原型链实现多态,即基于同一个方法签名在不同的子类中表现的形式不同:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

function WebSoftEngineer(n, a, s, h, lang) {

SoftEngineer.apply(this, [n, a, s, h, lang]);

this.Working = function () {

alert("我是网页工程师,从事网页开发设计工作!");

};

};

WebSoftEngineer.prototype = new SoftEngineer();

function AppSoftEngineer(n, a, s, h, lang) {

SoftEngineer.apply(this, [n, a, s, h, lang]);

this.Working = function () {

alert("我是应用工程师,从事客户端应用程序开发设计工作!");

};

};

AppSoftEngineer.prototype = new SoftEngineer();

var webengr = new WebSoftEngineer("梦在旅途", 29, "男", 178, "javascript");

webengr.Say();

webengr.Working();

var appengr = new AppSoftEngineer("梦在旅途", 29, "男", 178, "c#");

appengr.Say();

appengr.Working();

"javascript创建对象的几种常见方法"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report