admin管理员组文章数量:1319002
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?
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?
Share Improve this question asked Aug 20, 2010 at 22:29 NickNick 7,52521 gold badges79 silver badges129 bronze badges 2- Can you try by just moving the method notify above the ShipProduct method ? – Jagmag Commented Aug 20, 2010 at 22:31
- @In Sane - No, that doesn't work – Nick Commented Aug 23, 2010 at 14:15
4 Answers
Reset to default 8That 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 mas, 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();
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();
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>
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
};
本文标签: javascriptCalling one method from another in JSStack Overflow
版权声明:本文标题:javascript - Calling one method from another in JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742054211a2418201.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论