2012-02-03 82 views
1

這涉及到一個問題,我問以前在這裏:How to implement chained method calls like jQuery?JavaScript的OOP語法和對象既是VAR和功能

我一直在使用的方法從某些時候有檢查答案,這效果很好。但我想進一步改變我的工具箱的語法。

  1. foo(firstarg).bar(secondarg); // should function as the question above.

  2. foo(onlyarg).bar // a function with one argument

  3. foo.bar(onlyarg); // should also work, when a first argument is not appropriate.

  4. foo.bar; // a function without an argument, or returns a static value.

我想所有4個syntaxs工作過相同foo對象,但缺乏OOP理解。我已經嘗試了幾件事情,到目前爲止,我可以獲得1 & 2的工作,並且3 & 4能夠工作,但不是所有的工作都一起工作。如果每個函數都返回根對象,鏈接仍然是一個選項,那也會很好。

編輯:我顯然需要更具體的,這裏是我現在有:

var main = function(obj){ this.obj = obj; }; 
var tool = function(obj){ return new main(obj); }; 
main.prototype = { 
     alertThisPlus : function(plus){ 
      alert(this.obj + ' ' + plus);  
     }, 
     alertJustThis : function(){ 
      return alert(this.obj); 
     } 
}; 

使用

tool('hello').alertThisPlus('world'); // returns alert('hello world') 
tool().alertJustThis('hello world'); // returns alert('hello world'); 

我想的是要做到這一點:

tool('hello').alertThisPlus('world'); // returns alert('hello world') no change 
tool.alertJustThis('hello world'); // returns alert('hello world') does not work 
+0

這真的不是OOP ...但靜音點。除非'bar'是一個函數,否則3和4是不兼容的。你可以做的最好的事情是讓它們不帶參數地調用'bar'。同樣用1和2. – 2012-02-03 19:42:55

+1

沒錯。我已經有1和2個工作了,我已經有3和4個工作了。它讓所有的工作立即開始。我知道它可能是因爲jquery:$(arg).foo的作品,以及$ .browser例如。 – Fresheyeball 2012-02-03 19:49:07

回答

3

函數只是對象,這樣你就可以添加功能 '工具'。您可以手動執行此操作:

tool.foobar = function() {}; 

或者如果您的課程結構合適,您可以使用混合方法。這樣的事情:

function Tool(prefix) { 
    this.prefix = prefix; 
} 

Tool.prototype.alertThisPlus = function(suffix) { 
    alert((typeof this.prefix != 'undefined' ? this.prefix + ' ' : '') + suffix); 
}; 

Tool.prototype.alertJustThis = function(msg) { 
    alert(msg); 
}; 

function tool(prefix) { 
    return new Tool(prefix); 
} 

// Mix-in the methods from a static instance of Tool onto the 'tool' function. 
// This makes both tool.alertThisPlus() and tool.alertJustThis() available, 
// both will be called in the context of 'staticTool'. 
(function() { 
    var staticTool = new Tool(); 
    for (var o in staticTool) { 
    if (typeof staticTool[o] == 'function') { 
     tool[o] = staticTool[o].bind(staticTool); 
    } 
    } 
})(); 

tool('hello').alertThisPlus('world'); // returns alert('hello world') 
tool().alertJustThis('hello world'); // returns alert('hello world') 
tool.alertJustThis('hello world'); // returns alert('hello world') 
+0

請注意,所有瀏覽器都不支持'Function.bind()',因此您可能需要使用閉包或編寫自己的綁定函數。 – Dan 2012-02-03 20:56:03

+0

我瞭解除此部分以外的所有內容:(functionof()){static var toolTool = new Tool(); for(var o in staticTool)if(typeof staticTool [o] =='function'){ tool [o ] = staticTool [o] .bind(staticTool); } } })(); – Fresheyeball 2012-02-03 20:58:40

+0

你能解釋一下那裏發生了什麼嗎? – Fresheyeball 2012-02-03 20:58:52

0

我能想到的唯一方法就是允許你在沒有括號的情況下執行一個函數,如果你是a)將它作爲一個callb傳遞ack param,或者b)自己執行。

var foo = (function() { 
    alert('foo called'); 
})(); 

考慮到這一步,可以說你已經使用了一些閉包來返回值。

var foo = (function() { 
    return { 
    bar: "arf arf said the dog" 
    } 
})(); 

現在你可以使用:

foo.bar;