首页 > 编程语言 > 详细

面向对于javascript编程

时间:2016-07-23 18:20:51      阅读:242      评论:0      收藏:0      [点我收藏+]

以构造函数的方式定义对象

 function Person(name, age) {
        this.name = name;
        this.age = age;
        this.sayName = function () {
            alert(this.name);
        }
    }

    var person1 = new Person("wilson1", 10);
    var person2 = new Person("wilson2",20);

    Person("wilson3", 30);

    person1.sayName();
    person2.sayName();

    window.sayName();

 

 

 

定义对象属性

  var person = { _name: "", age: 0, Name: "" };
    Object.defineProperty(person, "name", {
        get: function () {
            return this._name;
        },
        set: function (newvalue) {
            this._name = newvalue;
        }
    });
    
    person.name = "wilson.fu";

 

 原型式定义对象

var Person = function (age, name) {
        this.age = age;
        this.name = name;
    }
    Person.prototype.name = "";
    Person.prototype.age = 0;
    Person.prototype.sayName = function () {
        alert(this.name);
    }
    //Person.prototype.name = "wilson";

    var person1 = new Person(10, "wilson1");
    person1.sayName();

    var person2 = new Person(20, "wilson2");

    person2.sayName();

 

详见:http://www.cnblogs.com/weiweictgu/p/5658996.html

 

面向对于javascript编程

原文:http://www.cnblogs.com/weiweictgu/p/5699152.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!