码迷,mamicode.com
首页 > Web开发 > 详细

js下的一种面向对象方法

时间:2015-04-03 17:15:01      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

本文记录了一种Javascript的面向对象方法,示例代码如下:

 

//构造函数
var MClass = function(value1, value2) {
    this.member = "hi";
    //定义成员属性
    Object.defineProperty(this, ‘member1‘, {
        value: value1
    });
    Object.defineProperty(this, ‘member2‘, {
        value: value2
    });
}

MClass.prototype.member0 = "bao";

//原型方法
MClass.prototype.sayHello = function() {
    console.log(this.member + " " + this.member0);
    console.log(‘hello  ‘ + this.member1 + ‘  And  ‘ + this.member2);
    return this;
}

//静态方法(类方法)
MClass.staticSayHello = function() {
    console.log(‘hello  ‘ + this.member0 + " " + this.member);
    return;
}


var entity = new MClass(‘fredric‘, ‘sinny‘);
MClass.staticSayHello();
entity.sayHello().sayHello();

 

执行结果:

hello  undefined undefined
hi bao
hello  fredric  And  sinny
hi bao
hello  fredric  And  sinny

 

js下的一种面向对象方法

标签:

原文地址:http://www.cnblogs.com/Fredric-2013/p/4390441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!