在 Visual Studio 2010 中使用从 Mindscape Web Workbench(coffeescript 源)生成的 javascript

发布于 2024-12-09 16:52:08 字数 1094 浏览 0 评论 0原文

此咖啡脚本代码

    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>

并尝试使用 with

    <script type="text/javascript">

    $(document).ready(function () {

        var coffee;

        coffee = TestCoffee("Jelouuuu");

        coffee.helloCoffee();

    });

</script>

我收到了 TestCoffee 未定义错误

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

提前致谢!

This coffee script code

    class TestCoffee
        constructor: (@saludo) ->

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

is generating the following javascript with mindscape web workbench 2.0.332.18684

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

How should I use this code inside my asp.net mvc 3 view?

I'm importing the js code with

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

and trying to use with

    <script type="text/javascript">

    $(document).ready(function () {

        var coffee;

        coffee = TestCoffee("Jelouuuu");

        coffee.helloCoffee();

    });

</script>

I got a TestCoffee undefined error

So, how should I use it?

Thanks In Advance!

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

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

发布评论

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

评论(2

习ぎ惯性依靠 2024-12-16 16:52:08

这是设计使然:

尽管为了清晰起见,本文档中隐藏了所有内容
CoffeeScript 输出包装在匿名函数中: (function(){
... })();这个安全包装器,结合自动生成
var 关键字,使得污染变得极其困难
意外的全局命名空间。

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

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

我通常使用 jQuery $ .extend 将函数推送到 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)

This is by design:

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/

I normally use jQuery $.extend to push a function into the jquery namespace for other scripts to use.

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)
め七分饶幸 2024-12-16 16:52:08

简单的改变就能让它发挥作用。在类声明中添加“@”符号:

class @TestCoffee
    constructor: (@saludo) ->

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

并更改实例化类的方式。在你的课堂上使用“新”

coffee = new TestCoffee("Jelouuuu");

应该可以得到你想要的。

Simple changes will allow this to work. Add a '@' symbol to your class declaration:

class @TestCoffee
    constructor: (@saludo) ->

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

And change the way you instantiate your class. Use a 'new' on your class

coffee = new TestCoffee("Jelouuuu");

That should get you what you want.

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