本文主要是介绍Javascript 创建对象的6种方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在Javascript开发过程中经常会遇见创建对象的时候;
下面我总结了6种常用创建对象的方法:
- 构造函数法
- 原型法
- 构造函数+原型法
- 动态原型法
- JSON法
- create法
1.构造函数法
其主要就是利用构造函数function来创建对象.
具体例子:
function Person(name, age, sex, phone)
{//prototythis.name = name;this.age = age;this.sex = sex;this.phone = phone;//methodthis.show = function(){console.log("name:" + this.name);console.log("age:" + this.age);console.log("sex:" + this.sex);console.log("phone:" + this.phone);};
};var person1 = new Person("Li",18,"man","123456");
var person2 = new Person("Wang",18,"women","123456");person1.show();
person2.show();
注意:里面的分隔符为分号,所有的元素,包括属性和函数前面都有this;
2.原型法
其主要就是利用prototype来创建对象
具体例子:
function Person1() {};Person1.prototype.name = "Li";
Person1.prototype.age = 18;
Person1.prototype.sex = "man";
Person1.prototype.phone = "123456";Person1.prototype.show = function(){console.log("name:" + this.name);console.log("age:" + this.age);console.log("sex:" + this.sex);console.log("phone:" + this.phone);
};function Person2(){};Person2.prototype.name = "Wang";
Person2.prototype.age = 18;
Person2.prototype.sex = "women";
Person2.prototype.phone = "123456";Person2.prototype.show = function(){console.log("name:" + this.name);console.log("age:" + this.age);console.log("sex:" + this.sex);console.log("phone:" + this.phone);
};var person1 = new Person1();
var person2 = new Person2();person1.show();
person2.show();
注意:很明显,这种方法每次使用一个对象时都要创建,因为它没有构造函数进行初始化;
它的关键在于属性,还有函数都是用了prototype;
3.构造函数+原型法
这就是上面两种方法的合并,用构造函数进行数值初始化,再利用prototype来定义函数
function Person(name, age, sex, phone)
{//propotythis.name = name;this.age = age;this.sex = sex;this.phone = phone;};//method
Person.prototype.show = function(){console.log("name:" + this.name);console.log("age:" + this.age);console.log("sex:" + this.sex);console.log("phone:" + this.phone);
};var person1 = new Person("Li",18,"man","123456");
var person2 = new Person("Wang",18,"women","123456");person1.show();
person2.show();
注意:function和prototype的结合;
4.动态原型法
这是原型法的进一步使用,其主要就是判断该数值或者该函数有没有被定义,如果有则不用重新定义
具体例子:
function Person(name, age, sex, phone)
{//propotythis.name = name;this.age = age;this.sex = sex;this.phone = phone;//methodif (Person.prototype.show === undefined){Person.prototype.show = function(){console.log("name:" + this.name);console.log("age:" + this.age);console.log("sex:" + this.sex);console.log("phone:" + this.phone);};}};var person1 = new Person("Li",18,"man","123456");
var person2 = new Person("Wang",18,"women","123456");person1.show();
person2.show();
注意:这里会判断是否定义;
5.JSON法
这种顾名思义,其创建方式就和JSON类似
具体例子:
var Person = {show : function() {console.log("name:" + this.name);console.log("age:" + this.age);console.log("sex:" + this.sex);console.log("phone:" + this.phone);},name : null,age : null,sex : null,phone : null,
};var person1 = Person;
var person2 = Person;person1.name = "Li";
person1.age = 18;
person1.sex = "man";
person1.phone = "123456";
person1.show();person2.name = "Wang";
person2.age = 18;
person2.sex = "women";
person2.phone = "123456";
person2.show();
注意,里面的分隔符要用逗号;
6.create法
这利用了对象的create方法进行创建
具体例子:
function createObject(objname,name, age, sex, phone){var person = new Object();person.objname = objname;person.name = name;person.age = age;person.sex = sex;person.phone = phone;person.show = function(){console.log("name:" + this.name);console.log("age:" + this.age);console.log("sex:" + this.sex);console.log("phone:" + this.phone);};return person;
}var person1 = createObject("person1","Li",18,"man","123456");
person1.show();var person2 = createObject("person2","Wang",18,"women","123456");
person2.show();
注意:用名字去创建一个对象,再进行初始化;
在工作中比较常用其实是:
1.构造函数+原型法
2.JSON法
3.create法
这篇关于Javascript 创建对象的6种方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!