我想要像 JQuery
使用的 $
符号一样的简写,以便快速引用在我的代码中广泛使用的对象。
如何做到这一点?
最佳答案
jQuery 中的
$
只是一个 JavaScript 变量名;
var $ = 5;
或者如果您想添加功能
var $ = {};
$.myMethod = function(){
console.log("Hello");
};
参见this book section关于命名空间和模块。
jQuery 在其源代码中做了什么:
// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;
他们只是将对对象 jQuery
的对象引用分配给 window.$
关于jquery - jQuery 库如何使用 $ 代替 "JQuery"来引用对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16382961/