简体   繁体   English

JS-使用原型调用静态方法

[英]JS - Call to static method with prototype

Well, I searched and didn't find a way to call a prototype method without instantiate another object. 好吧,我进行了搜索,但没有找到一种无需实例化另一个对象就可以调用原型方法的方法。 I found how to do: 我发现了怎么做:

 var x = new X();
 X.MyMethod();

What I really need to know is if there is a way to do like in jQuery, doing something like: 我真正需要知道的是,是否有一种方法可以在jQuery中进行类似的操作:

var x = "mystring";
x.MyMethod();

In jQuery UI, we call .draggable(), .resizable() in a selected object. 在jQuery UI中,我们在选定对象中调用.draggable()、. resizable()。 Can I do the same? 我可以一样吗?

You want to extend the String 's prototype methods? 您想扩展String的原型方法吗? You'll need to do something like this: 您需要执行以下操作:

String.prototype.MyMethod = function() {
    console.log("MyMethod()");
};

var x = "mystring";
x.MyMethod();

See also: How does JavaScript .prototype work? 另请参阅: JavaScript .prototype如何工作?

Or if you want to be really clever, study: MDN Reference: Define Property 或者,如果您想变得真正聪明,请研究: MDN参考:定义属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM