我是 CoffeeScript 新手,我喜欢 CoffeeScript 类,但不知道如何使用 jQuery.prototype 在 jQuery 中扩展它们。
这是我的应用程序中保存某种全局状态变量的部分,因此我想通过 $.myThing.myFunction() 调用它,而不是通常的 $.fn.extend/$().myThing() 扩展方式。
我可以让它像这样工作:
$ = jQuery
$.myThing = $.myThing || {}
$.extend $.myThing, {
myProperty: 0
myFunction: ->
}
这没问题,但是我不能将它用作类,而且结构看起来不太适合我的 pycharm。 (老实说,这可能比整个类(class)的事情更令人烦恼......)
我想要的是做这样的事情:
$ = jQuery
$.myThing = $.myThing || {}
class myThing
myProperty: 0
myFunction: ->
$.extend $.myThing, myThing()
但是它不起作用(除了在 pycharm 中看起来很漂亮之外)。有更好的方法吗?
最佳答案
这对你有用吗?
class jQuery.myThing
myProperty: 0
myFunction: ->
编译为:
jQuery.myThing = (function() {
function myThing() {}
myThing.prototype.myProperty = 0;
myThing.prototype.myFunction = function() {};
return myThing;
})();
编辑: $.myThing
作为类的实例:
class myThing
myProperty: 0
myFunction: ->
jQuery.myThing = new myThing
关于jquery - 使用 CoffeeScript 类扩展 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9600004/