javascript 对象

本文介绍了JavaScript中创建对象的多种方法,包括直接赋值、字面量方式、构造函数及原型模式,并探讨了原型链的概念及其应用。

对象

第一种是直接创建一个新的空对象,然后通过对象给它的属性赋值

用分号隔开

<script>
    /*创建一个新的对象,然后赋值*/
    var person = new Object();
    person.name  = "金前旺"
    person.pwd = 123;
    person.age = 23;
    person.show = function shownName(){
        alert(this.name)
    }
    person.show();
第二种是直接用字面量赋值的方式来进行创建,其中用逗号隔开
    /*第二种创建方式*/
    var person = {
        name:"金前旺",
        pwd:123,
        age:23,
        show:function showName(){
            alert(this.name)
        }
    }
    person.show()
</script>

数组Array 的concat()

构造函数

/*构造函数*/
function Person( name ,age ,pwd){
    this.name = name;
    this.age = age ;
    this.pwd = pwd;
    this.show= function showInfo(){
        document.write(this.name+"<br/>"+this.age+"<br/>"+this.pwd)
    }
}
var person = new Person("jing",23,"12345")
person.show();
var person1 = new Person("jing",23,"12345")
person1.show();

 

Constructor比较上下级关系(严格

alert(person.constructor==Person)              true

alert(person.constructor==Object)              false

instanceof 比较上下级关系

alert( person. instanceof==Person)               true

alert( person. instanceof==Object)               true

 

原型模式生产prototype(实现数据的共享)

<script>
    /*实现数据的共享,不用模型的话,每个对象都是新的实例对象,
    但有了原型模式以后,每个对象都是共享的同一个原型对象*/
    function Person(){
    }
    Person.prototype.name = "金前旺"
    Person.prototype.age = 23
    Person.prototype.pwd = "12345"
    Person.prototype.show = function (){
        /*这两种写法都是一样的*/
       /* alert(this.name)*/
        alert(Person.prototype.name)
    }
    var person = new Person();
    person.show();
    var person1 = new Person()
    person1.show();
 
    //这里提示值是true,说明他们共享的是同一个对象
    alert(person.show==person1.show)
</script>
 

假如要动态的改变一个值,也就将它写在构造方法中,不须改变的共享值就写在原型中,这样的话通过构造和原型最大限度的节约了资源

/*实现数据的共享,不用模型的话,每个对象都是新的实例对象,
但有了原型模式以后,每个对象都是共享的同一个原型对象*/
function Person(){
}
function Person(name , email){
    this.name = name;
    this.email = email;
}
Person.prototype.name = "金前旺"
Person.prototype.age = 23
Person.prototype.pwd = "12345"
Person.prototype.show = function (){
    /*这两种写法都是一样的*/
   /* alert(this.name)*/
    alert(Person.prototype.name)
}
var person = new Person("jin","123@qq.com")
var person1 = new Person("wo","000@qq.com")
alert(person.name+"<br/>"+person1.name+"</br>"+person.email+"</br>"+person1.email+"</br>"+person.age+"</br>"+person1.age)
/*输出值*/
/*jin<br/>wo<br/>123@qq.com<br/>000@qq.com<br/>23<br/>23*/




原型链

<!--<script>
    function Person(){
        this.cloths = ["1","2","3"],
                this.head=1;
        this.foot=2;
    }
    function Student(name,age){
        this.name = name;
        this.age = age;
    }
    /*解决原型混乱的方法*/
    Student.prototype = new Person();

    var s1 = new Student();
    /*这样的话打印的原型构造就是父类的Person了*/
    /*个人感觉这种方法*/
    alert(s1.constructor)
    /*然后通过一个中间的空对象类继承父类的对象,相互转换*/
</script>-->



<script>
    /*只要不是基本数据类型,引用数据类型用特定方法来改变,就会改变父类的原型共享*/
    function Person(){
        this.cloths = ["1","2","3"],
       /* this.head=1;
        this.foot=2;*/
                this.head;
        this.foot;
    }
    Person.prototype.head = 1;
    Person.prototype.foot = 2;
    function Student(){
        this.name;
        this.age;
    }
    Student.prototype = new Person();

    var student = new Student("jin",23);
    student.head = 3;
    /*用这种方法的话,共享数组并不会被改变*/
    student.cloths = ["1","2","3","4"]
    /*用着种方法的话,就会改变共享的数组*/
    student.cloths.push("4")

    var student1 = new Student("金","12");
    alert(student1.head);
    alert(student1.cloths)
    /*属性不会改变,但是实例*/

//    /*调用call和apply方法来借用父类的构造方法*/
//    function Person(){
    /*这种方法的话就等于直接创建了一个新的内存地址*/
//        this.cloths = ["1","2","3"];
//        this.head=1;
//        this.foot=2;
//    }
//    function Student(){
//        /*二选一即可*/
//        Person.apply(this);
//        Person.call(this);
//        this.name;
//        this.age;
//    }
//    Student.prototype = new Person();
//    /*call和apply*/
//    var s1 = new Student();
//    s1.cloths.push("4");
//    alert(s1.cloths);
//    var s2 = new Student();
//    alert(s2.cloths);
//
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值