Javascript 创建对象的6种方法

在Javascript开发过程中经常会遇见创建对象的时候;

下面我总结了6种常用创建对象的方法:

  • 构造函数法
  • 原型法
  • 构造函数+原型法
  • 动态原型法
  • JSON法
  • create法

1.构造函数法

其主要就是利用构造函数function来创建对象.

具体例子:

function Person(name, age, sex, phone)
{
    //prototy
    this.name  = name;
    this.age   = age;
    this.sex   = sex;
    this.phone = phone;

    //method
    this.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)
{
    //propoty
    this.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)
{
    //propoty
    this.name  = name;
    this.age   = age;
    this.sex   = sex;
    this.phone = phone;

    //method
    if (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法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值