简体   繁体   English

在JS中从另一个调用一个方法

[英]Calling one method from another in JS

I have the following snippet of JS 我有以下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
    }
}

How would I call Notify from ShipProduct? 我怎么称呼ShipProduct的通知?

That is not JS, that is a collection of syntax errors. 不是JS,而是语法错误的集合。

Use = when assigning variables, and : inside simple objects, don't confuse simple objects and functions, don't forget commas, and don't prefix property names with this. 在分配变量时使用= ,在简单对象内部使用: ,不要混淆简单对象和函数,不要忘记逗号,也不要在属性名称前添加前缀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();

This seems to work: 这似乎可行:

<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>

This example looks fine except for the first line, that colon should be an equal sign. 除了第一行,该示例看起来还不错,冒号应该是等号。

The problem, I'm guessing, has to do with how you're calling ShipProduct . 我猜,这个问题与您如何称呼ShipProduct If you're doing it like this, everything should work: 如果您这样做的话,那么一切都会正常进行:

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

However if you detach the method and call it directly, it won't work. 但是,如果您分离该方法并直接调用它,它将无法工作。 Such as: 如:

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

This is because JavaScript relies on the dot notation accessor to bind this . 这是因为JavaScript依靠点表示法访问器来绑定 this I'm guessing that you're passing the method around, perhaps to an Ajax callback or something. 我猜想您正在传递该方法,也许是传递给Ajax回调之类的东西。

What you need to do in that case it wrap it in a function. 在这种情况下,您需要做的是将其包装在一个函数中。 Such as: 如:

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

How about: 怎么样:

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

This assumes that you want to expose both functions -- if notify is only used by Customer internally, then there's no need to expose it, so you would instead return like so: 假设您要公开这两个函数-如果notify仅由Customer内部使用,则无需公开它,因此您可以这样返回:

    return {
        shipProduct: shipProduct
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM