js 实现继承功能(类似java extends)
2012-03-13 11:15
633 查看
<script type="text/javascript">
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Function.method('inherits', function (parent) {
var d = {}, p = (this.prototype = new parent());
this.method('uber', function uber(name) {
if (!(name in d)) {
d[name] = 0;
}
var f, r, t = d[name], v = parent.prototype;
if (t) {
while (t) {
v = v.constructor.prototype;
t -= 1;
}
f = v[name];
} else {
f = p[name];
if (f == this[name]) {
f = v[name];
}
}
d[name] += 1;
r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));
d[name] -= 1;
return r;
});
return this;
});
function Parenizor(value) {
this.setValue(value);
}
Parenizor.method('setValue', function (value) {
this.value = value;
return this;
});
Parenizor.method('getValue', function () {
return this.value;
});
Parenizor.method('toString', function () {
return '(' + this.getValue() + ')';
});
myParenizor = new Parenizor(11);
myString = myParenizor.toString();
console.log(myString);
function ZParenizor(value) {
this.setValue(value);
}
ZParenizor.inherits(Parenizor);
ZParenizor.method('toString', function () {
if (this.getValue()) {
return this.uber('toString');
}
return "-0-";
});
myZParenizor = new ZParenizor(12);
myString = myZParenizor.toString();
console.log(myString);
</script>
http://javascript.crockford.com/inheritance.html
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Function.method('inherits', function (parent) {
var d = {}, p = (this.prototype = new parent());
this.method('uber', function uber(name) {
if (!(name in d)) {
d[name] = 0;
}
var f, r, t = d[name], v = parent.prototype;
if (t) {
while (t) {
v = v.constructor.prototype;
t -= 1;
}
f = v[name];
} else {
f = p[name];
if (f == this[name]) {
f = v[name];
}
}
d[name] += 1;
r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));
d[name] -= 1;
return r;
});
return this;
});
function Parenizor(value) {
this.setValue(value);
}
Parenizor.method('setValue', function (value) {
this.value = value;
return this;
});
Parenizor.method('getValue', function () {
return this.value;
});
Parenizor.method('toString', function () {
return '(' + this.getValue() + ')';
});
myParenizor = new Parenizor(11);
myString = myParenizor.toString();
console.log(myString);
function ZParenizor(value) {
this.setValue(value);
}
ZParenizor.inherits(Parenizor);
ZParenizor.method('toString', function () {
if (this.getValue()) {
return this.uber('toString');
}
return "-0-";
});
myZParenizor = new ZParenizor(12);
myString = myZParenizor.toString();
console.log(myString);
</script>
http://javascript.crockford.com/inheritance.html
相关文章推荐
- JS实现类似 继承功能
- 微信js-sdk+JAVA实现“分享到朋友圈”和“发送给朋友”功能详解
- JS类似JAVA的replaceAll()的实现
- 用C语言写一个数组,实现类似JAVA语言中ArrayList的功能
- 原生js实现类似队列的功能
- 原生JS不到30行,实现类似javascript MVC的功能-minTemplate
- 在python project中实现类似java project里的properties文件功能
- JAVA + js 实现 头像上传及裁剪功能
- js中实现多态采用和继承类似的方法
- java如何实现类似c语言的条件汇编功能
- js实现类似php中strtotime函数和timetostr的日期转换/互换功能
- java web中实现同一帐号同一时间只能一个地点登陆(类似QQ登录的功能)
- js 实现类似 Java 中的 StringBuilder
- java中实现类似函数指针的功能
- C++实现类似JAVA反射的基本功能
- JS自定义对象实现Java中Map对象功能的方法
- javascript实现类似java的继承,单个方法继承
- Java利用接口和类来实现类似函数指针的功能
- js+java 实现图片在线预览功能