jquery源码研究(原型继承)
来源:互联网 发布:phpstorm apache 编辑:程序博客网 时间:2024/06/07 23:55
jquery源码研究(原型继承)
观看者:javascript,jquery爱好者。需要一定的继承基础,如果没有可以看我的javascript深入了解(继承)
目标:了解jquery原型继承,实现jquery选择器的基础 $("id"),$("input") ...
实现方式:代码及相关文字解释。从最简单方式逐步推进。
首先用一个实现一个简单的实现:$("id") 返回一个jquery对象有一个方法tagName();于是我想到这样的:
- $ = window.JQuery = function(id){
- return new JQuery(id);
- }
那么我们可不可以new出一个另外的对象返回呢?
- $ = window.JQuery = function(id){
- return new JQuerylite(id);
- }
- JQuerylite = function(id){
- this.dom = document.getElementById("dv");
- this.tagName = function(){
- alert(this.dom.tagName);
- }
- }
- $("dv").tagName();
- $ = window.JQuery = function(id){
- return new JQuery.prototype.init(id);
- }
- JQuery.prototype = {
- init:function(id){
- this.dom = document.getElementById("dv");
- this.tagName = function(){
- alert(this.dom.tagName.toLowerCase());
- };
- this.version = '1.71';
- },
- version:'1.7',
- name:'JQuery'
- }
- alert($("dv").version);//1.71
- alert($("dv").name);//undefined
- $("dv").tagName();//div
我前面的javascript深入了解(面向对象)里面也讲过,这里我再回顾一遍吧,当我们new 出来的JQuery.prototype.init(id),其实是调用的是JQuery.prototype.init的构造方法,如此一来我们共享的原型应该是JQuery.prototype.init.prototype = {},所以我们这里调用不到了,不过还有个方法是想法子将JQuery的prototype赋值给JQuery.prototype.init.prototype就行了。下面看例子:
- $ = window.JQuery = function(id){
- return new JQuery.prototype.init(id);
- }
- JQuery.prototype = {
- init:function(id){
- this.dom = document.getElementById("dv");
- this.tagName = function(){
- alert(this.dom.tagName.toLowerCase());
- };
- this.version = '1.71';
- },
- version:'1.7',
- name:'JQuery'
- }
- JQuery.prototype.init.prototype = JQuery.prototype;
- alert($("dv").version);//1.71
- alert($("dv").name);//undefined
- $("dv").tagName();//div
实现以$(selector,context).size() 得到dom个数,selector 可以是id或者是标签名,context 是范围。
- (function(){
- $ = window.JQuery = function(selector,context){
- return new JQuery.prototype.init(selector,context);
- }
- JQuery.prototype = {
- init:function(selector,context){
- var selector = selector ? selector : document;
- var context = context ? context : document;
- var dom;
- if(dom = context.getElementById(selector)){
- this[0] = dom;
- this.length = 1;
- }else{
- dom = context.getElementsByTagName(selector);
- for(var i=0; i<dom.length; i++){
- this[i] = dom[i];
- }
- this.length = dom.length;
- }
- },
- version:'1.7',
- size:function(){
- return this.length;
- }
- }
- JQuery.prototype.init.prototype = JQuery.prototype;
- })();
- alert($("div").size());//3
- alert($("dv").size());//1
- jquery源码研究(原型继承)
- jquery源码研究(原型继承)
- jQuery源码研究分析学习笔记-jQuery原型属性和方法(九)
- jQuery源码研究学习笔记(二)
- jQuery创建实例与原型继承揭秘
- jQuery源码研究分析学习笔记-jQuery.buildFragment()(六)
- jQuery源码学习之五 (jQUery继承方法)
- jQuery源码阅读(八)---jQuery中的继承extend
- 原型对象与继承(原型链)
- jQuery原型方法.pushStack源码分析
- Javascript继承(原型链)
- 破解 JS(原型)继承
- javascript原型继承(1)
- javascript原型继承(1)
- javascript arguments对象研究--针对jquery源码研究再研究
- 原型继承
- 原型继承
- 原型继承
- OpenCV计算连通区域数目与最大连通区域并标示出
- java中hibernate的Serializable解析
- 闹钟学习
- HDU 4549 M斐波那契数列
- 国企程序员的那些事儿
- jquery源码研究(原型继承)
- 移动平台的meta标签-----神奇的功效
- hadoop namenode
- Linux操作命令总结(三)
- C# 有放回(重复)组合算法
- 瑞丽市中国公民赴缅甸须知
- ubuntu root 密码忘记怎么办?
- hdu 1014
- linux下如何查看硬盘型号等信息