简体   繁体   English

jQuery fn中的“prototype”? + jquery函数内的普通javascript代码?

[英]“prototype” in jQuery fn? + plain javascript code inside a jquery function?


i was wondering what prototype means in jQuery? 我想知道jQuery中的原型意味着什么? i usually find infos about "Prototype" - the framework - but here it's "prototype" inside jQuery...? 我通常会找到关于“Prototype”的信息 - 框架 - 但是这里是jQuery里面的“原型”......? Could you please tell me when it's best to use it? 你能告诉我什么时候最好用吗?

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? 另外,你能告诉我:为什么我们在jQuery插件中使用“普通javascript”代码而不是jquery代码,就像在这个例子中一样? is it a rapidity issue? 这是一个快速问题吗?

here's my example : (a plugin for twitter) 这是我的例子:(一个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);  
    });  
};  

I was wondering what "prototype" means in jQuery 我想知道jQuery中“原型”是什么意思

The JavaScript library named Prototype takes its name from the JavaScript prototype . 名为Prototype的JavaScript库的名称来自JavaScript原型 In JavaScript, the prototype is the mechanism which provides inheritance (as opposed to, say, classes). 在JavaScript中, 原型是提供继承的机制(与类相反)。 Example: String.prototype refers to the prototype object of String s, which contains (among other things) methods which can be invoked on any String . 示例: String.prototype引用String s的prototype对象,其中包含可以在任何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: 更多关于原型的阅读:


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. jQuery不是JavaScript的替代品。 It is a JavaScript library, meant to make it easier to write JavaScript which would otherwise have to use the DOM API. 它是一个JavaScript库,旨在使编写JavaScript更容易, 否则必须使用DOM API。 You see "vanilla" JavaScript in the jQuery plugin because there's really no better way to do what that JS does. 你在jQuery插件中看到“vanilla”JavaScript,因为实际上没有更好的方法来做JS所做的事情。

jQuery is JavaScript. jQuery是JavaScript。 It simplifies writing other JavaScript by providing a simpler API which remains consistent across browsers. 它通过提供更简单的API来简化编写其他JavaScript的过程,这些API在浏览器中保持一致。 This is not the case with the DOM, because the various browser vendors have created DOM APIs which are mostly the same, but not identical. 这不是DOM的情况,因为各种浏览器供应商已经创建了大多数相同但不相同的DOM API。

Quirksmode is a site popular for documenting these inconsistencies (in CSS as well as in JavaScript). Quirksmode是一个流行的网站,用于记录这些不一致性(在CSS和JavaScript中)。


Welcome to web development! 欢迎来到网站开发!

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

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

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