JS中定义对象的几种方式

    1. 基于已有对象扩充其对象和方法(只适合于临时的生成一个对象):

<html>
<head>
    <title>基于已有对象扩充其对象和方法(只适合于临时的生成一个对象)</title>
</head>
<script>
    var obj = new Object();
    obj.name="userObject";
    obj.printName = function(name){
        this.name = name;
        alert(this.name);
    }
    obj.printName("newUserObject");
</script>
<body>
</body>
</html>

 

    2.工厂方式创建对象(每次创建一个object后,其中对应的方法(本例中是get)也被创建一次,方法不能被所创建的所有对象(本例中是obj1和obj2)共享):

a. 

<html>
<head>
    <title>工厂方式创建对象</title>
</head>
<script>
    function createObject(){
    var obj = new Object();
    obj.name="userObject";
    obj.password = "12345";
    obj.get = function(){
        alert(this.name + ", " + this.password);
    }
    return obj;
    }
    var obj1 = createObject();
    var obj2 = createObject();
    obj1.get();
    obj2.get();
</script>
<body>
</body>
</html>

b.带参数的构造方法:

<html>
<head>
    <title>工厂方式创建对象</title>
</head>
<script>
    function createObject(username,password){
    var obj = new Object();
    obj.username = username;
    obj.password = password;
    obj.get = function(){
        alert(this.username + ", " + this.password);
    }
    return obj;
    }
    var obj1 = createObject("zhangsan","123456");
    obj1.get();
</script>
<body>
</body>
</html>

 

c.让函数被多个创建的对象所共享,而不是每一个对象都创建一个相同的函数(缺点:对象定义本身和方法分离了):

<html>
<head>
    <title>工厂方式创建对象</title>
</head>
<script>
    function get(){
    alert(this.username + ", " + this.password);
    }
   
    function createObject(username,password){
        var obj = new Object();
        obj.username = username;
        obj.password = password;
        obj.get = get;
        return obj;
    }
   
    var obj1 = createObject("zhangsan","123456");
    var obj2 = createObject("lisi","54321");
    obj1.get();
    obj2.get();
</script>
<body>
</body>
</html>

 

    3.构造函数方式:

a.

<html>
<head>
    <title>构造函数方式创建对象</title>
</head>
<script>
   
    function Person(){
    this.username = "zhangsan";
    this.password = "123";
    this.getInfo = function(){
        alert(this.username + ", " + this.password);
    }
    }
   
    var person = new Person();
    person.getInfo();
</script>

<body>
</body>
</html>

b.可以在构造函数是传递参数:

<html>
<head>
    <title>构造函数方式创建对象</title>
</head>
<script>
   
    function Person(username,password){
    this.username = username;
    this.password = password;
    this.getInfo = function(){
        alert(this.username + ", " + this.password);
    }
    }
    var person = new Person("zhangsan","123");
    person.getInfo();
</script>

<body>
</body>
</html>

 

    4.使用原型(prototype)方式创建对象:

优点:创建的多个对象共享同一个方法(getInfo)

缺点:创建的多个对象在共享同一个方法的同时也共享了同样的属性(username,password),实际开发中这样儿是不行的,必须是多个对象都要有自己的属性。

采用该方式创建对象通常是用来扩展已有对象的某一个方法。

a.

<html>
<head>
    <title>使用原型(prototype)方式创建对象</title>
</head>
<script>
    
    function Person(){}
    Person.prototype.username = "zhangsan";
    Person.prototype.password = "123";
    Person.prototype.getInfo = function(){
        alert(this.username + ", " + this.password);
    }
    var person = new Person();
    var person2 = new Person();
    person.username = "lisi";
    person.getInfo();
    person2.getInfo();
</script>

<body>
</body>
</html>

 

b.如果使用原型方式创建对象,那么生成的所有对象将会共享原型中的属性,这样儿一个对象改变了该属性也会反应到其他的对象上:

<html>
<head>
    <title>使用原型(prototype)方式创建对象</title>
</head>
<script>
   
    function Person(){}
    Person.prototype.username = new Array(0);
    Person.prototype.password = "123";
    Person.prototype.getInfo = function(){
        alert(this.username + ", " + this.password);
    }
    var person = new Person();
    var person2 = new Person();
    person.username.push("zhangsan");
    person.username.push("lisi");
    person.password = "456";
    person.getInfo();
    person2.getInfo();
</script>

<body>
</body>
</html>

 

c.使用原型和构造函数方式来创建对象:

<html>
<head>
    <title>使用原型(prototype)方式创建对象</title>
</head>
<script>
   
    function Person(){
    this.username = new Array();
    this.password = "123"
    }
    Person.prototype.getInfo = function(){
        alert(this.username + ", " + this.password);
    }
    var person = new Person();
    var person2 = new Person();
    person.username.push("zhangsan");
    person.username.push("lisi");
    person.getInfo();
    person2.getInfo();
</script>

<body>
</body>
</html>

 

    5.动态原型方式:在构造函数中通过标志量让所有对象共享一个方法,而每个对象拥有自己的属性。

<html>
<head>
    <title>动态原型方式:在构造函数中通过标志量让所有对象共享一个方法,而每个对象拥有自己的属性</title>
</head>
<script>
   
    function Person(){
    this.username = "zhangsan";
    this.password = "123";
    if(typeof Person.flag == "undefined"){
        Person.prototype.getInfo = function(){
            alert(this.username + ", " + this.password);
        }
        Person.flag = true;
    }
    }
    var p = new Person();
    var p2 = new Person();
    p.getInfo();
    p2.getInfo();
    p.username="lisi";
    p.getInfo();
    p2.getInfo();
</script>

<body>
</body>
</html>

你可能感兴趣的:(js)