jQuery fn 中的“原型”?+ jquery 函数中的纯 javascript 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5767361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"prototype" in jQuery fn? + plain javascript code inside a jquery function?
提问by Paul
i was wondering what prototype means in jQuery? i usually find infos about "Prototype" - the framework - but here it's "prototype" inside jQuery...? Could you please tell me when it's best to use it?
我想知道原型在 jQuery 中意味着什么?我通常会找到有关“原型”的信息 - 框架 - 但这里是 jQuery 中的“原型”......?你能告诉我什么时候最好用吗?
Also, could you tell me : why do we use a "plain javascript" code inside a jQuery plugin, instead of the jquery code, like in this example? is it a rapidity issue?
另外,你能告诉我:为什么我们在 jQuery 插件中使用“普通 javascript”代码,而不是像这个例子中的 jquery 代码?这是一个速度问题吗?
here's my example : (a plugin for twitter)
这是我的示例:(推特插件)
Thanks for your answers !
感谢您的回答!
$.each(data, function(i, item) {
holder.append("<p>"+item.text.makeLinks()+"</p>");
});
//...further in the code
String.prototype.makeLinks = function() {
? ? return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(str) {
? ? ? ? return str.link(str);
? ? });
};
回答by Matt Ball
I was wondering what "prototype" means in jQuery
我想知道 jQuery 中的“原型”是什么意思
The JavaScript library named Prototype takes its name from the JavaScript prototype. In JavaScript, the prototypeis the mechanism which provides inheritance (as opposed to, say, classes). Example:String.prototype
refers to the prototype
object of String
s, which contains (among other things) methods which can be invoked on any String
.
名为 Prototype 的 JavaScript 库的名称来自JavaScript 原型。在 JavaScript 中,原型是提供继承的机制(与类相反)。示例:String.prototype
指的prototype
是String
s的对象,其中包含(除其他外)可以在任何String
.
In the example in your question, the twitter plugin creates a new function on String.prototype
, called makeLinks
, which allows for function calls such as:
在您问题的示例中,twitter 插件在String.prototype
,上创建了一个新函数,称为makeLinks
,它允许函数调用,例如:
text.makeLinks()
where text
is a string.
哪里text
是一个字符串。
More reading on prototypes:
更多关于原型的阅读:
- How does JavaScript .prototype work?
- How are prototype functions different than normal functions in javascript ?
- Understanding prototype method calls
- Understanding JavaScript prototypes
Why do we use a "plain javascript" code inside a jQuery plugin, instead of the jQuery code?
为什么我们在 jQuery 插件中使用“普通 javascript”代码,而不是 jQuery 代码?
jQuery is not a replacement for JavaScript. It is a JavaScript library, meant to make it easier to write JavaScript which would otherwise have to use the DOM API.You see "vanilla" JavaScript in the jQuery plugin because there's really no better way to do what that JS does.
jQuery 不是 JavaScript 的替代品。它是一个 JavaScript 库,旨在使编写 JavaScript 变得更容易,否则必须使用 DOM API。您会在 jQuery 插件中看到“普通”JavaScript,因为确实没有更好的方法来完成 JS 所做的事情。
jQuery is JavaScript.It simplifies writing other JavaScript by providing a simpler API which remains consistent across browsers. This is not the case with the DOM, because the various browser vendors have created DOM APIs which are mostlythe same, but not identical.
jQuery 是 JavaScript。它通过提供在浏览器间保持一致的更简单的 API 来简化编写其他 JavaScript 的过程。DOM 的情况并非如此,因为各种浏览器供应商都创建了 DOM API,它们大多相同,但并不完全相同。
Quirksmodeis a site popular for documenting these inconsistencies (in CSS as well as in JavaScript).
Quirksmode是一个流行的站点,用于记录这些不一致(在 CSS 和 JavaScript 中)。
Welcome to web development!
欢迎来到网页开发!
回答by Ortiga
Prototype is a pure javascript functionality.
Prototype 是一个纯 javascript 功能。
It's used to add properties or methods to an already defined object (somewhat like extending). For example:
它用于向已定义的对象添加属性或方法(有点像扩展)。例如:
function myCustomObject(){
}
myCustomObject.prototype.myCustomAttr = "Hello";
var instance = new myCustomObject();
alert(instance.myCustomAttr); // alerts Hello