English 中文(简体)
贾瓦伦工厂模式
原标题:Factory Pattern in JavaScript

我想从正在使用的法典中分离出 Java语,以便我能够灵活地把一个物体的执行换成具有相同签名的其他物体,同时又不触及守则的许多内容。 为了实现这一目标,我提出了保存概念和制造物体的工厂方法。 执行:

//The Factory Method
function ObjectFactory() {}
ObjectFactory.create = function (o) {
    var args = [].slice.call(arguments, 1);

    function F() {}
    F.prototype = o.prototype;
    var instance = new F();
    o.apply(instance, args);
    return instance;
};

//The Repository
var Repository = {
     invitation : Invitation,
     message : Message
};

//Usage
var inv = ObjectFactory.create(Repository["invitation"], "invitation", "invitation body", "Sender");
var msg = ObjectFactory.create(Repository["message"], "message", "message body");
var inv2 = ObjectFactory.create(Repository["invitation"], "invitation2", "invitation body2", "Sender");

这一模式对我来说是行之有效的,但在我的项目中,在我开始执行这一守则之前,我想知道,在使用这一方法的情况下,存在着任何陷阱(如果我制造5-10个200-1000条代码线的物体),制造物体、性能瓶颈。 我在长时间内就服务器边代码开展工作后,回到了 Java,因此我对我的解决方案没有信心。 而且,我本可以使用ES5目标。 而现在客户则与IE8和FF3.6浏览器 st。

Thanks

问题回答

Just use Object.create() along with an ES5 shim like this: https://github.com/kriskowal/es5-shim/blob/master/es5-shim.js

It does most of what you want, and does things the ES5 way for when that actually becomes standard. Given the common case of using one object as the prototype for another, it works fine in all browsers.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签