Frage

Ich habe folgendes Snippet JS

var Customer : function()
{
    this.ShipProduct : function()
    {
       //Logic for shipping product. If shipping successful, notify user
       //Here I am trying to call Notify
       //this.Notify(); // does not work
    }

    this.Notify = function()
    {
      //Logic for notify
    }
}

Wie würde ich diesen Artikel Anruf von ShipProduct?

War es hilfreich?

Lösung

Das ist nicht JS, die eine Sammlung von Syntaxfehlern ist.

Verwenden = wenn Variablen und : innerhalb einfacher Objekte zuweisen, nicht zu verwechseln einfache Objekte und Funktionen, keine Kommas vergessen und nicht Präfix Eigenschaftsnamen mit this..

var Customer = {
    ShipProduct : function()
    {
       //Logic for shipping product. If shipping successful, notify user
       //Here I am trying to call Notify
       this.Notify(); // this does work
    },
    Notify: function()
    {
      //Logic for notify
    }
}

Customer.ShipProduct();

Andere Tipps

In diesem Beispiel sieht in Ordnung, außer für die erste Zeile, dass Doppelpunkt sollte ein Gleichheitszeichen sein.

Das Problem, ich bin zu raten, hat damit zu tun, wie Sie ShipProduct sind aufgerufen wird. Wenn Sie es so tun, sollte alles funktionieren:

var customer = new Customer();
customer.ShipProduct();

Wenn Sie jedoch detach das Verfahren und es direkt aufrufen, es wird nicht funktionieren. Wie zum Beispiel:

var customer = new Customer();
var shipMethod = customer.ShipProduct;
shipMethod();

Das ist, weil JavaScript verlässt sich auf die Punktnotation Accessor bind this. Ich vermute, dass Sie die Methode um vorbei sind, vielleicht zu einem Ajax-Rückruf oder so etwas.

Was Sie in diesem Fall tun müssen, sie wickeln Sie es in einer Funktion. Wie zum Beispiel:

var customer = new Customer();
var shipMethod = function() {
    customer.shipMethod();
};
... later, in some other context ...
shipMethod();

Das scheint zu funktionieren:

<html>
<head>
<script type = "text/javascript" language = "JavaScript">
var Customer = function(){
    this.ShipProduct = function(){
        alert("hey!");
        this.Notify();
    };

    this.Notify = function(){
      //Logic for notify
        alert("notify");
    };
};
</script>
</head>
<body>
<script type = "text/javascript" language = "JavaScript">
var cust = new Customer();
cust.ShipProduct();
</script>
</body>
</html>

Wie wäre:

var Customer = function() {
    var notify = function() {
        ...
    };
    var shipProduct = function() {
        ...
        notify(...);
        ...
    };
    return {
        notify: notify,
        shipProduct: shipProduct
    };
}

Dies setzt voraus, dass Sie beide Funktionen verfügbar machen möchten - wenn notify nur durch Customer intern verwendet wird, dann gibt es keine Notwendigkeit, es zu belichten, so dass Sie stattdessen wie so zurückkehren würde:

    return {
        shipProduct: shipProduct
    };
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top