admin管理员组文章数量:1291797
I recently started using coffeescript and was curious what is the "right" way to expose an object that I create with Coffeescript to other javascript pages. Because of coffeescripts wrapping functionality, is it acceptable behavior to call window.coffeeObject = externalObject
.
Example
example.coffee
externalObject =
method1: -> 'Return value'
method2: -> 'Return method2'
window.myApi = externalObject
example.js -- piled from example.coffee
(function() {
var externalObject;
externalObject = {
method1: function() {
return 'Return value';
},
method2: function() {
return 'Return method2';
}
};
window.myApi = externalObject;
}).call(this);
other.js
alert(myApi.method1()) // Should return "Return value"
I recently started using coffeescript and was curious what is the "right" way to expose an object that I create with Coffeescript to other javascript pages. Because of coffeescripts wrapping functionality, is it acceptable behavior to call window.coffeeObject = externalObject
.
Example
example.coffee
externalObject =
method1: -> 'Return value'
method2: -> 'Return method2'
window.myApi = externalObject
example.js -- piled from example.coffee
(function() {
var externalObject;
externalObject = {
method1: function() {
return 'Return value';
},
method2: function() {
return 'Return method2';
}
};
window.myApi = externalObject;
}).call(this);
other.js
alert(myApi.method1()) // Should return "Return value"
Share
Improve this question
asked May 13, 2011 at 17:18
brentbrent
1,7292 gold badges13 silver badges16 bronze badges
2
- it should work...does it raise some errors? are you sure you load other.js after example.js? – stecb Commented May 13, 2011 at 17:25
- I'm sure it works, I think he's just asking for best practices here. – Alex Wayne Commented May 13, 2011 at 17:26
2 Answers
Reset to default 5Yep that's correct. Alternatively you can use define @myApi = { foo: -> }
because this
is window
in the root context of the file.
You can simplify the syntax further, for example if you had 2 internal functions
example.coffee
myPrivateFunction = ->
"return 1"
myPrivateFunction2 = ->
"return 2"
@myApi = {
myFunction : myPrivateFunction,
myFunction2 : myPrivateFunction2
}
example.js
this.myApi = {
myFunction: myPrivateFunction,
myFunction2: myPrivateFunction2
};
The @
will be window
within the main scope of the file.
Then call from elsewhere by window.myApi.myFunction()
If you wanted to map the external function names to the same internal names, if you don't specify key : value
pairs, it will just use the string value as the key by default.
example.coffee
@myApi = {
myPrivateFunction,
myPrivateFunction2
}
example.js
this.myApi = {
myPrivateFunction: myPrivateFunction,
myPrivateFunction2: myPrivateFunction2
};
本文标签: Expose a javascript api with coffeescriptStack Overflow
版权声明:本文标题:Expose a javascript api with coffeescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741527623a2383563.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论