需要的模式:创建返回可执行函数的新对象,并从原型继承 [英] Pattern needed: create new object that returns an executeable function and inherits from a prototype

查看:47
本文介绍了需要的模式:创建返回可执行函数的新对象,并从原型继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景1 - 一切正常:

Scenario 1 - everything works:

var AwesomeObject = function()
{
   var self = this;
   self.whatstuff = 'really awesome';
}

AwesomeObject.prototype.doStuff = function()
{
   var self = this;
   console.log('i did '+self.whatstuff+' stuff');
   return self;
}

var awesome = new AwesomeObject(); //returns a new AwesomeObject
awesome.doStuff(); // prints 'i did really awesome stuff' on the console

现在我想要它甚至更棒:

Now i want it even awesomer:

var AwesomeObject = function()
{  
   var f = function() { console.log('i am awesome'); }
   var self = f;
   self.whatstuff = 'really awesome';
   return self;
}

AwesomeObject.prototype.doStuff = function()
{
   var self = this;
   console.log('i did '+self.whatstuff+' stuff');
   return self;
}

var awesome = new AwesomeObject(); //returns the interal f object
awesome(); // prints 'i am awesome'
awesome.doStuff(); // throws an error

新的AwesomeObject本身应该返回一个可执行函数,这样我就可以说'真棒' ();'

new AwesomeObject should return an executable function itself, so that i can say 'awesome();'

但我希望它继承 AwesomeObject.prototype

添加 self.prototype = AwesomeObject.prototype; 无效。

var AwesomeObject = function()
{  
   var f = function() { console.log('i am awesome'); }
   var self = f;
   self.whatstuff = 'really awesome';
   self.prototype =  AwesomeObject.prototype;
   return self;
}

好的我可以复制 AwesomeObject.prototype 函数 - 一个接一个 - 进入 f的范围f

ok i can copy the AwesomeObject.prototype functions - one after the other - into the scope of f

var AwesomeObject = function()
{  
   var f = function() { console.log('i am awesome'); }
   var self = f;
   self.whatstuff = 'really awesome';
   self.doStuff =  function() { AwesomeObject.prototype.doStuff.apply(self,arguments); }
   return self;
}

但我认为必须有更好的方法,更好的模式,什么是它?

but i think there must be a better way, a better pattern, what is it?

这个问题让我发疯,帮助真的很感激。

this issue drives me crazy, help would be really appreciated.

一般来说:如何创建一个

in general: how to create a function object that


  • 的函数对象with new

  • 返回一个可以执行的函数对象

  • 继承给定原型的所有属性和方法

有办法吗?

thx
Franz

thx Franz

推荐答案

一个非常简单的模式是工厂。

A very simple pattern is a factory.

var AwesomeObject = (function() {
    var AwesomeObject = function() {
        this.whatstuff = 'really awesome';
    };

    AwesomeObject.prototype.doStuff = function() {
        console.log('i did ' + this.whatstuff + ' stuff');
        return this;
    };

    return function() {
        var o = new AwesomeObject();
        var f = function() { console.log("I am awesome"); };
        for (var k in o) {
            f[k] = o[k];    
        }

        return f;
    };

})();

var foo = AwesomeObject();
foo();
foo.doStuff();

实例

这个想法是你将你的功能和你的对象分成两件事。您的对象存在于函数的本地范围内,函数可以使用该对象。

The idea is that you seperate your function and your object into two things. Your object exists in the local scope of your function and the function can use the object.

对象本身完全通过原型继承。

The object itself inherits completely through the prototype.

关键是将对象的所有属性/方法转发到函数上。

The key is do forward all properties/methods of the object onto the function.

这是最干净的解决方案。

This is the cleanest solution.

这篇关于需要的模式:创建返回可执行函数的新对象,并从原型继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆