Javascript prototype字段是否存在语法糖?

Javascript prototype字段是否存在语法糖?,javascript,prototype,Javascript,Prototype,有没有办法清理我的javascript代码中的所有Obj.prototype.field=语句?我正在使用jQuery。我想做同样的事情,只是我的代码中没有其他单词是prototype。也许我问的是不可能的——如果是的话,就告诉我,这就是答案 我发现这是一个难以回答的问题,因为有一个名为“Prototype”的js库…可能只是: Obj.prototype = { run: function(){}, fooBar: function(test1,test2){} }; 可能只是:

有没有办法清理我的javascript代码中的所有
Obj.prototype.field=
语句?我正在使用jQuery。我想做同样的事情,只是我的代码中没有其他单词是
prototype
。也许我问的是不可能的——如果是的话,就告诉我,这就是答案

我发现这是一个难以回答的问题,因为有一个名为“Prototype”的js库…

可能只是:

Obj.prototype = {
  run: function(){},
  fooBar: function(test1,test2){}  
};
可能只是:

Obj.prototype = {
  run: function(){},
  fooBar: function(test1,test2){}  
};
?使用。第二个参数的属性将复制到第一个参数:

$.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 );
        });
   }, ...
使用。第二个参数的属性将复制到第一个参数:

$.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 );
        });
   }, ...
看看jQuery和YUI3等库的“扩展”功能

http://yuilibrary.com/yui/docs/yui/yui-extend.html

还可以看看merge(jQuery,YUI3)和augment(YUI3)及其用法。

看看jQuery和YUI3等库的“扩展”功能

http://yuilibrary.com/yui/docs/yui/yui-extend.html


还可以看看merge(jQuery,YUI3)和augment(YUI3)以及用法

var p = Obj.prototype;

p.newMethod = function(window.alert("Hi! I live"));
p.newProperty = false;

你也可以这样做

var p = Obj.prototype;

p.newMethod = function(window.alert("Hi! I live"));
p.newProperty = false;

Coffeescript是相当好的语法糖。是的,不幸的是咖啡不是一个选项。Coffeescript是相当好的语法糖。是的,不幸的是咖啡不是一个选项。缺点-如果适用的话:这会覆盖整个原型对象。您还将丢失到超类的链接,以及以前定义的任何属性和方法。这将替换
Obj.prototype
,并丢失所有其他属性。缺点-如果适用,这将覆盖整个prototype对象。您还将丢失到超类的链接,以及以前定义的任何属性和方法。这将替换
Obj.prototype
,并丢失所有其他属性。嗯,是的。我的意思是在那里有
var
。谢谢,嗯,是的。我的意思是在那里有
var
。谢谢