[英]Pseudoclassical Inheritance
I am currently working through Javascript the good parts which is a amazingly good book but as the heading saying I seem to be hitting a dead end getting the code to work for chapter five. 我目前正在研究Javascript的优秀部分,这是一本了不起的好书,但是正如标题所言,我似乎在为第五章工作的代码方面陷入僵局。 When I run the code I am getting nothing in return so I'm not sure where its wrong as its just like the book, any help is great. 当我运行代码时,我没有得到任何回报,因此我不确定它的错误之处,就像本书一样,任何帮助都很好。
Function.method('new',function(){
var that = Object.beget(this.prototype);
var other = this.apply(that,arguments);
return (typeof other === 'object' && other) || that ;
});
var Mammal = function(name){
this.name = name;
};
Mammal.prototype.get_name = function(){
return this.name;
};
Mammal.prototype.says = function(){
return this.saying || '';
}
var myMammal = new Mammal('Herb the mammal');
var name = myMammal.get_name();
document.writeln(name);
Please refer 'Augmenting Types' in Chapter 'Functions' of 'JavaScript - The Good Parts'. 请参阅“ JavaScript-优质零件”的“功能”一章中的“扩增类型”。
Please add the following code prior to the code posted in the question. 请在问题中发布的代码之前添加以下代码。
This will make the method
function available on Function
. 这将使method
函数在Function
可用。
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Please refer this fiddle for the complete working code. 请参阅此小提琴以获取完整的工作代码。
Please note new
method is already available on Function
. 请注意, Function
上已经提供了new
方法。 The code displays how to augment
. 该代码显示了如何augment
。
You can get rid of the following blocks from your code. 您可以从代码中摆脱以下块。
Function.method('new',function(){
var that = Object.beget(this.prototype);
var other = this.apply(that,arguments);
return (typeof other === 'object' && other) || that ;
});
But should you need to retain the above block , you have to augment
method
function to Function
. 但是,如果您需要保留上述块 ,你必须augment
method
功能Function
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.