欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jquery初步认识-20140123

程序员文章站 2022-07-12 21:30:48
...
一、原型模式结构

// 定义一个jQuery构造函数
var jQuery = function() {
    xxxxxxxxxx
};

// 扩展jQuery原型 
jQuery.prototype = {
    xxxxxxxxxx
};

//上面是一个原型模式结构,一个jQuery构造函数和jQuery实例化对象的的原型对象

 var jq = new jQuery(); //变量jq通过new关键字实例化jQuery构造函数后就可以使用原型对象中的方法,但是jQuery不是这个用的


二、返回选择器实例

var jQuery = function() {
    // 返回选择器实例
    return new jQuery.prototype.init();
};
jQuery.prototype = {
    // 选择器构造函数
    init: function() {

    }
};

虽然jQuery不是通过new关键字实例化对象,但是执行jQuery函数仍然得到的是一个通过new关键字实例化init选择器的对象

var navCollections = jQuery('.nav');  //变量navCollections保存的是class名为nav的DOM对象集合


三、访问原型方法

var jQuery = function() {
    // 返回选择器实例
    return new jQuery.prototype.init();
};
jQuery.prototype = {
    // 选择器构造函数
    init: function() {

    },

    // 原型方法
    toArray: function() {

    },
    get: function() {

    }    
};

// 共享原型
jQuery.prototype.init.prototype = jQuery.prototype;

jQuery函数中返回的选择器实例对象为jQuery对象

jQuery('.nav').toArray(); // 将结果集转换为数组

jQuery.prototype.init.prototype = jQuery.prototype; // 共享原型