有什么方法可以清理我的 JavaScript 代码中的所有 Obj.prototype.field =
语句吗?我正在使用 jQuery。我想做同样的事情,除了我的代码中的所有其他单词不是原型(prototype)。也许我所问的是不可能的——如果是这样,请告诉我,这就是答案。
我发现这是一个谷歌无法搜索的问题,因为有一个名为“Prototype”的 js 库...
最佳答案
使用jQuery.extend 。第二个参数的属性将被复制到第一个参数:
$.extend(Obj.prototype,{
field1: "hi there",
func1: function(){
//do stuff
}
});
这是 jQuery 内部使用的,jQuery.fn 是 jQuery.prototype 的别名:
jQuery.fn.extend({
attr: function( name, value ) {
return jQuery.access( this, jQuery.attr, name, value, arguments.length > 1 );
},
removeAttr: function( name ) {
return this.each(function() {
jQuery.removeAttr( this, name );
});
}, ...
关于javascript - Javascript 原型(prototype)字段是否存在任何语法糖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12144020/