码迷,mamicode.com
首页 > 编程语言 > 详细

面向对于javascript编程

时间:2016-07-23 18:21:55      阅读:167      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!