简体   繁体   English

jQuery喜欢自定义函数

[英]jQuery like custom functions

I have been wondering how I can create functions like jQuery. 我一直想知道如何创建像jQuery这样的函数。 For example: $(ID).function() 例如: $(ID).function()

Where ID is the id of an HTML element, $ is a function that return the document.getElementById reference of the element "ID" and function is a custom javascript function. 其中ID是HTML元素的id, $是返回元素“ID”的document.getElementById引用的函数,函数是自定义javascript函数。

I'm creating a little library which implements some functions. 我正在创建一个实现一些功能的小库。 And I want to use that sintax without using jQuery. 我想在不使用jQuery的情况下使用那个sintax。

Now, my questions are: how I can implement that? 现在,我的问题是:我如何实现这一点? What is the name of the tecnique that allow that? tecnique允许的名称是什么?

Edit: 编辑:

What I want to do is this: 我想要做的是:

HTMLElement.prototype.alertMe = function() {alert(this.value);} HTMLElement.prototype.alertMe = function(){alert(this.value);}

Then, when I call document.getElementById('html_input_id').alertMe() , it must show an alertbox with the input value. 然后,当我调用document.getElementById('html_input_id').alertMe() ,它必须显示一个带有输入值的警告框。 But HTMLElement.prototype doesn't work in IE. 但是HTMLElement.prototype在IE中不起作用。

$ = function(id) {
    return document.getElementById(id);
}

Okay, look, what you're asking has a lot of details and implications. 好的,看,你问的问题有很多细节和含义。 The code for jQuery is open source, you can read it for the details; jQuery的代码是开源的,您可以阅读它的详细信息; you'd do well to find a good Javascript book as well, the the O'Reilly Definitive Guide. 你也可以找到一本好的Javascript书,O'Reilly权威指南。

$ is just a character for names in JS, so as some of the other answers have shown, there's no reason you can't just write a function with that name: $只是JS中名字的一个字符,所以正如其他一些答案所示,没有理由你不能只用这个名字写一个函数:

var $ = function(args){...}

Since everyone and his brother uses that trick, you want to have a longer name as well, so you can mix things. 由于每个人和他的兄弟都使用这个技巧,你也希望有一个更长的名字,所以你可以混合使用。

var EstebansLibrary = function(args){...}
var $ = EstebansLibrary; // make an alias

Since you end up doing different things with the entry point function, you need to know how JS uses arguments -- look up the arguments object. 由于您最终使用入口点函数执行不同的操作,因此您需要知道JS如何使用参数 - 查找arguments对象。

You'll want to package this so that your internals don't pollute the namespace; 你需要打包这个,这样你的内部不会污染命名空间; you'll want some variant of the module pattern , which will make it something like 你会想要一些模块模式的变体,它会使它变得类似

var EstebansLibrary = (function(){
    // process the arguments object
    // do stuff
    return {
       opname : implementation,...
    }
})();

And you'll eventually want to be prepared for inheritance and that means putting those functions into the prototype object. 并且您最终希望为继承做好准备,这意味着将这些函数放入prototype对象中。

You can use prototype to assign a new function to the Element prototype. 您可以使用prototype为Element原型分配新函数。

Element.prototype.testFunction=function(str){alert(str)}; Element.prototype.testFunction =函数(STR){警报(STR)};

This would provide the function 'testFunction' to all HTML elements. 这将为所有HTML元素提供函数'testFunction'。

You can extend any base Object this way, ie Array, String etc. 您可以通过这种方式扩展任何基础对象,即Array,String等。

This will work without any plugin at all - although that said I don't think it will work in IE. 这将完全没有任何插件 - 虽然这说我认为它不会在IE中工作。 I believe libraries such as MooTools and jQquery create their own inheritance with DOM elements to ensure cross-browser compatibility, although don't quote me on that. 我相信像MooTools和jQquery这样的库使用DOM元素创建自己的继承,以确保跨浏览器兼容性,尽管不引用我。

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

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