使用在Visual Studio 2010中从mindscape web工作台(coffeescript源)生成的jav

Using javascript generated from mindscape web workbench (coffeescript source) in Visual Studio 2010

本文关键字:coffeescript jav 工作台 web Visual Studio 2010 mindscape 中从      更新时间:2023-09-26

咖啡脚本代码

    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视图?

我用

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

并试图与

一起使用
    <script type="text/javascript">
    $(document).ready(function () {
        var coffee;
        coffee = TestCoffee("Jelouuuu");
        coffee.helloCoffee();
    });
</script>

I got a TestCoffee undefined error

那么,我应该如何使用它呢?

提前感谢!

这是故意的:

尽管为了清晰起见,在本文档中没有显示,但所有CoffeeScript输出被封装在一个匿名函数中:(function(){…}) ();这种安全包装,结合了自动生成的var关键字,使其极其难以污染全局命名空间。

如果你想创建顶级变量供其他脚本使用,将它们作为属性附加在窗口上,或附加在导出对象上CommonJS。存在运算符(将在下面介绍)为您提供一个找出在哪里添加它们的可靠方法;如果你同时瞄准这两个目标CommonJS和浏览器:导出?这个

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

我通常使用jQuery $。扩展将函数推入jquery名称空间以供其他脚本使用。

IE:

file1.coffee

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

file2.coffee

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

简单的更改将允许这工作。在你的类声明中添加一个"@"符号:

class @TestCoffee
    constructor: (@saludo) ->
    helloCoffee: ->
        alert @saludo+" Coffee v7"

并改变实例化类的方式。在你的类上使用new

coffee = new TestCoffee("Jelouuuu");

应该能得到你想要的。