2016-05-05 5 views
-1

implementieren Wie kann ichWie Vorfunktion in JavaScript

callMethod(); // Works 
callMethod.doThisWay(); // Still works 
+0

Nun, was hast du probiert? Da Funktionen Eigenschaften enthalten können, sollte der triviale Ansatz funktionieren. – doldt

+0

Randnotiz: Eine Unterfunktion wird als "(statische) Methode" bezeichnet – Oisin

Antwort

1

Sure Unterfunktionen in JavaScript implementieren können Sie :-)

Schreiben Sie einfach

const callMethod = function() { 
    // ... 
}; 

callMethod.doThisWay = function() { 
    // ... 
}; 

und fertig: -)

0

Das funktioniert auch:

var callMethod = function() { 
    this.doThisWay = function() { 
    alert('doThisWay'); 
    } 

    alert('callMethod'); 

    return this; 
}; 

var a = new callMethod(); 
a.doThisWay(); 

gibt es auch andere Möglichkeiten, dies zu tun.