gpt4 book ai didi

javascript - 在 Visual Studio 2010 中使用从 mindscape 网络工作台(coffeescript 源)生成的 javascript

转载 作者:行者123 更新时间:2023-11-29 10:22:57 25 4
gpt4 key购买 nike

这个 CoffeeScript 代码

    class TestCoffee
constructor: (@saludo) ->

helloCoffee: ->
alert @saludo+" Coffee v7"

正在使用 mindscape web workbench 2.0.332.18684 生成以下 javascript

    (function() {
var TestCoffee;
TestCoffee = (function() {
function TestCoffee(saludo) {
this.saludo = saludo;
}
TestCoffee.prototype.helloCoffee = function() {
return alert(this.saludo + " Coffee v7");
};
return TestCoffee;
})();
}).call(this);

我应该如何在我的 asp.net mvc 3 View 中使用这段代码?

我正在导入 js 代码

<script src="@Url.Content("~/Scripts/helloCoffe.js")" type="text/javascript"></script>

并尝试使用 with

    <script type="text/javascript">

$(document).ready(function () {

var coffee;

coffee = TestCoffee("Jelouuuu");

coffee.helloCoffee();

});

</script>

我遇到了 TestCoffee 未定义的错误

那么,我该如何使用呢?

提前致谢!

最佳答案

这是设计使然:

Although suppressed within this documentation for clarity, all CoffeeScript output is wrapped in an anonymous function: (function(){ ... })(); This safety wrapper, combined with the automatic generation of the var keyword, make it exceedingly difficult to pollute the global namespace by accident.

If you'd like to create top-level variables for other scripts to use, attach them as properties on window, or on the exports object in CommonJS. The existential operator (covered below), gives you a reliable way to figure out where to add them; if you're targeting both CommonJS and the browser: exports ? this

http://jashkenas.github.com/coffee-script/

我通常使用 jQuery $.extend 将函数推送到 jquery 命名空间以供其他脚本使用。

即:

文件1.咖啡

$ ->
$.extend
ErrorAlert: (text) ->
simpleGritter $("#SuccessGritter"), text
return false

文件2.咖啡

$ ->
$("#right").ajaxError (event, request, settings) ->
$.ErrorAlert("Some kind of bad thing happened at: " + settings.url)

关于javascript - 在 Visual Studio 2010 中使用从 mindscape 网络工作台(coffeescript 源)生成的 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7730242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com