标签:
本文记录了一种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
标签:
原文地址:http://www.cnblogs.com/Fredric-2013/p/4390441.html