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

发布于 2024-09-15 12:36:22 字数 377 浏览 7 评论 0原文

我有以下 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
    }
}

我如何从 ShipProduct 调用 Notify?

I have the following snippet of 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别把无礼当个性 2024-09-22 12:36:22

那不是JS,那是语法错误的集合。

分配变量时使用 =,简单对象内使用 :,不要混淆简单对象和函数,不要忘记逗号,不要在属性名称前添加 <代码>这个。。

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();

That is not JS, that is a collection of syntax errors.

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

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();
不必你懂 2024-09-22 12:36:22

这个例子看起来不错,除了第一行,冒号应该是等号。

我猜这个问题与您调用 ShipProduct 的方式有关。如果您这样做,一切都应该有效:

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

但是,如果您分离该方法并直接调用它,它将不起作用。如:

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

这是因为 JavaScript 依赖于点表示法访问器来绑定 this。我猜测您正在传递该方法,也许是传递给 Ajax 回调或其他东西。

在这种情况下你需要做的就是将其包装在一个函数中。例如:

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

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. 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. I'm guessing that you're passing the method around, perhaps to an Ajax callback or something.

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();
若有似无的小暗淡 2024-09-22 12:36:22

这似乎有效:

<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 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>
若水般的淡然安静女子 2024-09-22 12:36:22

怎么样:

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

这假设您想要公开这两个函数 - 如果 notify 仅由 Customer 在内部使用,则无需公开它,因此您可以返回像这样:

    return {
        shipProduct: shipProduct
    };

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:

    return {
        shipProduct: shipProduct
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文