首页 > Web开发 > 详细

js下的一种面向对象方法

时间:2015-04-03 17:14:57      阅读:267      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!