【问题标题】:Javascript's super constructor - clarification?Javascript超级构造函数 - 澄清?
【发布时间】:2014-06-04 17:56:02
【问题描述】:

我有 been watching this video 达米安说 Crockford 称之为:“超级构造器模式”

代码示例:(来自视频)

var signalR;

signalR = function (url){
 return new signalR.prototype.init(url);
}

signalR.prototype={
 init:function(url)
 {
   this.url=url;
 } 
}

signalR.prototype.init.prototype = signalR.prototype;

现在,我用谷歌搜索了 Crockford 和超级构造函数我只能找到 Object.create 的实现:

我理解得很清楚:(也是陷阱)

function create(o)
{
 function f(){};
 f.prototype=o;
 return new f();
}

但我仍然不明白它是如何关联的:

问题:

  • 究竟是什么(在视频中) - 他是否尝试使用这种模式来解决问题? (也非常感谢小代码示例)。

【问题讨论】:

  • 此模式唯一启用的是您可以在没有new 的情况下调用signalR。另见stackoverflow.com/questions/15591434/…
  • @Bergi 我相信还有一些东西:例如 - 他实例化了 init 方法。所以init是ctor函数.....你确定这是唯一的吗?如果您将其转换为答案,也将不胜感激。(附注:链接有点真实,但不是 100%,因为这里,正如我所说,他将 init 作为 ctor 函数处理)
  • 是的,他使用init作为构造函数,这是一个糟糕的模式。
  • @Bergi 请我在这里学习。你能告诉我为什么这么可怕吗?或者您能否提供一个描述此确切模式的链接?
  • 有时作为一种模式(谁会使用new jQuery() 而不仅仅是$()?),有时只是为了在有人忘记在构造函数调用前添加new 时发现错误。 This also seeming to be the reason why Crockford dislikes new.

标签: javascript signalr


【解决方案1】:

让我们看看普通类的构造函数和原型

//-------------------
//---- Class signalr
//-------------------

//-- constructor
var signalr=function () {}

//--prototype
signalr.prototype.init=function (url)
    {
    this.url=url;
    }

signalr.prototype.test=function () {alert ("test");}

//-------------------
//---- Class signalr -- END
//-------------------

所以要生成这个类的新实例,我们必须编写以下内容。

var con=new signalr ();  //--- con would inherit two methods init and test
con.init ("yoururl");

让我们看看克罗克福德

//-------------------
//---- Class signalr - Crockford 
//-------------------

//-- signalr is here not the constructor, it's a normal function with a return value and setted prototype, the constructor becomes init which is defined in the signalr prototype
var signalr=function (url) 
    {
    return new signalr.prototype.init (url);
    }

//--prototype
//-----------

   //-- constructor
signalr.prototype.init=function (url)
    {
    this.url=url;
    }

signalr.prototype.test=function () {alert ("test");}
signalr.prototype.init.prototype=signalr.prototype       //- with this line, the prototype of the init method which is the constructor of our instances is linked to the prototype signalR so all instances would inherit the properties and methods defined in the signalR prototype

//-------------------
//---- Class signalr -- END
//-------------------

所以要生成这个类的新实例,我们可以编写以下代码来实现与上面相同的效果。

var con=signalr ("yourURL");                //--- con inherits all the methods and properties of signalr.prototype with one line

似乎克罗克福德懒得写行,我正在考虑实际的例子。但我认为整件事没什么令人兴奋的

【讨论】:

  • 我也看不出这会带来什么。为什么不在真正的构造函数中调用this.init()
  • 比你还得写 var con=new signalr("yourURL") ;))) 正如我所说的,我认为他在写行方面很懒惰。但这就是我的看法,也许有一些隐藏的东西,但我不这么认为
  • @ThorstenArtner'Austria' 我不知道他为什么提到克罗克福德。我没有找到一篇关于他谈论这种模式的文章。有链接吗?
  • 不,我什至不知道克罗克福德是谁,我依赖你的问题,这来自克罗克福德。
  • @ThorstenArtner'Austria' 我不明白这一点。为什么不 jsut 像这样jsbin.com/vajoredo/2/edit 那样做 ctor 功能和新功能?
猜你喜欢
  • 1970-01-01
  • 2014-09-02
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多