简体   繁体   English

伪古典继承

[英]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.

相关问题 使用伪经典继承设置prototype属性 - Setting prototype property with pseudoclassical inheritance 通过伪经典实例化(JavaScript)掌握原型继承 - Grasping prototypical Inheritance through pseudoclassical instantiation (JavaScript) Crockford的伪经典继承部分中的函数构造函数 - Function constructor in Crockford's pseudoclassical inheritance section 伪经典继承相对于功能继承(工厂函数)有哪些技术优势? - What are the technical advantages of pseudoclassical inheritance over functional inheritance (factory functions)? 使用“伪经典”模式在JavaScript中实现成员变量继承 - Achieving member variable inheritance in JavaScript using “pseudoclassical” pattern 关于何时在Crockford的Pseudoclassical Inheritance示例中使用原型的困惑 - Confusion about when to use prototype in Crockford's example of Pseudoclassical Inheritance 尝试使用伪经典继承创建BST时递归函数出现问题 - Issues with a recursive function when trying to create a BST using Pseudoclassical Inheritance 在ajax之后使用Pseudoclassical实例化 - use the Pseudoclassical instantiation after ajax Javascript Prototypal instantiation vs Pseudoclassical instantiation - Javascript Prototypal instantiation vs Pseudoclassical instantiation 原型继承中的“多重继承” - “Multiple inheritance” in prototypal inheritance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM