js 创建对象的两种主要方法 构造函数 和 原型+构造函数组合

来源:互联网 发布:pulltorefresh.js 编辑:程序博客网 时间:2024/06/05 20:58
    //重点掌握创建对象的方法--1(构造函数模式)    function Person(name,age,job) {        this.name=name;        this.age=age;        this.job=job;        this.saiHi=function (laugh) {            alert("laugh")        }    }    var p1=new Person("jack",15,"clerk");    p1.saiHi();   //laugh     alert(p1.name);  //jack



    //重点掌握创建对象的方法--2(构造函数+原型 组合模式)    function Person(name,age,job) {        this.name=name;        this.age=age;        this.job=job;        this.friends=["lily","lucy","marry"]    }    Person.prototype={        constructor:Person,        sayName:function () {            alert(this.name)        }    };    var p2=new Person("jerry",18,"student");    p2.friends.push("black");    alert(p2.friends); //lily,lucy,marry,black    p2.sayName();     //jerry




阅读全文
0 0
原创粉丝点击