一、我们从一个简单的构造函数+原型程序开始

 var G = function(){};
G.prototype = {
length : 5,
size : function(){
return this.length;
}
}

上例是个非常简单的程序,如果需要调用,我们可以用new的方式

var oG = new G();
console.log( oG.size() ); //5
1、常见的错误调用方式1
console.log( G.size() ); //报错
G.size这种调用,是把size当做静态方法调用,如果需要正常的调用, 应该把size方法加在函数本身,如:
G.size = function(){
return 10;
}
2、常见的错误调用方式2
G().size() //报错
G()返回的是undefined, undefined.size() 肯定是报错, size是函数G原型对象上的方法,如果要确保这种方式调用正常,那么我们就要让G() 返回一个G函数的实例
所以,我们可以这样做:
         var G = function () {
if( this instanceof G ) {
return this;
}else {
return new G();
}
};

把G的构造函数改造一下,判断this是否是当前G函数的实例,如果是,直接返回,如果不是,返回new G()  这样根据原型对象的查找原则,就能确保调用到size方法

完整的代码:

         var G = function () {
if( this instanceof G ) {
return this;
}else {
return new G();
}
};
G.prototype = {
length: 5,
size: function () {
return this.length;
}
}
console.log( G().size() );

在jquery框架中,他是怎么做的?

         var G = function () {
return G.fn;
};
G.fn = G.prototype = {
length: 5,
size: function () {
return this.length;
}
}
console.log( G.prototype.size() ); //
console.log( G().size() ); //

在jquery中, 为函数G增加一个属性fn( 记住:在js中函数是对象,这其实就是给对象增加了一个属性 ),然后在G函数中返回 G.fn 就是就是返回G.prototype

那么用G().size 其实就是相当于调用 G.prototype.size()

二、在jquery中,这个构造函数一般是用来选择元素的。

G返回的是G的原型对象,我们要增加选择元素的功能,自然而然,就是往原型对象上添加:

         var G = function ( id ) {
return G.fn.init( id );
};
G.fn = G.prototype = {
init : function( id ){
return document.getElementById( id );
},
length: 5,
size: function () {
return this.length;
}
}

向G的原型对象上添加一个init方法, 然后在构造函数中调用

         window.onload = function(){
console.log( G( 'box' ) );
G('box').style.backgroundColor = 'red';
// G('box').size(); //报错,无法链式调用
} <div id="box">ghost wu tell you how to learn design pattern</div>

虽然通过init方法,能够选择到dom元素,但是不能实现链式调用, 因为G('box')返回的是一个dom对象, 而在dom对象上是没有size这个方法的,因为size是G.prototype上的

所以要实现链式调用,就要确保init方法返回的是G的实例或者G.prototype,  这个时候,this就可以派上用场了

 <script>
var G = function (id) {
return G.fn.init(id);
};
G.fn = G.prototype = {
init: function (id) {
this[0] = document.getElementById(id);
this.length = 1;
return this;
},
length: 0,
size: function () {
return this.length;
}
}
window.onload = function () {
console.log(G('box'));
console.log( G('box2').size() );
}
</script>
     <div id="box">ghost wu tell you how to learn design pattern</div>
<div id="box2">id为box2的第二个div</div>

把选择到的元素放在this中, 这个时候的this指向的是G.fn,G.prototype?

因为在构造函数中,是这样调用的: G.fn.init( id ), 我把G.fn标成红色, 也就是相当于G.fn是一个对象,没错他确实就是一个对象G.prototype,所以在init中的this指向的就是

init方法前面的对象( G.fn, G.prototype ).

三、this覆盖

接下来,就会产生一个问题, this共用之后,元素选择就会产生覆盖

 <script>
var G = function (id) {
return G.fn.init(id);
};
G.fn = G.prototype = {
init: function (id) {
this[0] = document.getElementById(id);
this.length = 1;
console.log( this === G.fn, this === G.prototype, this );
return this;
},
length: 0,
size: function () {
return this.length;
}
} window.onload = function(){
console.log( G( 'box' ) );
console.log( G( 'box2' ) );
}
</script> <div id="box">ghost wu tell you how to learn design pattern</div>
<div id="box2">id为box2的第二个div</div>

调用两次构造函数G 去获取元素的时候, this[0] 现在都指向了 id为box2的元素, 把第一次G('box')选择到的id为box的元素覆盖了,产生覆盖的原因是this共用,那么我们

可以通过什么方法把this分开呢?不同的this指向不同的实例? 用什么? 恩,对了, 用new,每次new一个构造函数就会生成新的实例

四、解决this覆盖与链式调用

     <script>
var G = function (id) {
return new G.fn.init(id);
};
G.fn = G.prototype = {
init: function (id) {
this[0] = document.getElementById(id);
this.length = 1;
return this;
},
length: 0,
size: function () {
return this.length;
}
}
window.onload = function(){
console.log( G( 'box' ) );
console.log( G( 'box2' ) );
}
</script>
<div id="box">ghost wu tell you how to learn design pattern</div>
<div id="box2">id为box2的第二个div</div>

通过构造函数中new G.fn.init( id ) 的方式,每次生成一个新的实例,但是产生了一个新的问题,不能链式调用, 因为init中的this发生了改变,不再指向( G.fn, G.prototype ).

G('box2').size();//报错,
为了能够调用到size(), 所以在执行完构造函数之后,我们要确保this指向G的实例,或者G的原型对象,
只需要把init函数的原型对象指向G.fn就可以了
         var G = function (id) {
return new G.fn.init(id);
};
G.fn = G.prototype = {
init: function (id) {
this[0] = document.getElementById(id);
this.length = 1;
return this;
},
length: 0,
size: function () {
return this.length;
}
}
G.fn.init.prototype = G.fn;

加上G.fn.init.prototype = G.fn;我们就修正了this的覆盖与链式调用问题

五、扩展选择器

上面支持id选择器,我们只需要在init函数加上其他类型的扩展就可以了,比如,我这里增加了一个标签选择器

 <!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div,p {
border:1px solid red;
margin: 10px;
padding: 10px;
}
</style>
<script>
var G = function ( selector, context ) {
return new G.fn.init( selector, context );
};
G.fn = G.prototype = {
constructor : G,
init: function ( selector, context ) {
this.length = 0;
context = context || document;
if ( selector.indexOf( '#' ) == 0 ) {
this[0] = document.getElementById( selector.substring( 1 ) );
this.length = 1;
}else {
var aNode = context.getElementsByTagName( selector );
for( var i = 0, len = aNode.length; i < len; i++ ){
this[i] = aNode[i];
}
this.length = len;
}
this.selector = selector;
this.context = context;
return this;
},
length: 0,
size: function () {
return this.length;
}
}
G.fn.init.prototype = G.fn; window.onload = function(){
console.log( G('#box')[0] );
var aP = G('p', G('#box')[0]);
// var aP = G('p');
// var aP = G('#p1');
for( var i = 0, len = aP.size(); i < len; i++ ){
aP[i].style.backgroundColor = 'blue';
}
}
</script>
</head> <body>
<div id="box">
<p>跟着ghostwu学习设计模式</p>
<p>跟着ghostwu学习设计模式</p>
<p>跟着ghostwu学习设计模式</p>
<p>跟着ghostwu学习设计模式</p>
</div>
<p id="p1">跟着ghostwu学习设计模式</p>
<p>跟着ghostwu学习设计模式</p>
</body> </html>

[js高手之路] 设计模式系列课程 - jQuery的链式调用与灵活的构造函数的更多相关文章

  1. [js高手之路] 设计模式系列课程 - jQuery的extend插件机制

    这里在之前的文章[js高手之路] 设计模式系列课程 - jQuery的链式调用与灵活的构造函数基础上增加一个extend浅拷贝,可以为对象方便的扩展属性和方法, jquery的插件扩展机制,大致就是这 ...

  2. [js高手之路]设计模式系列课程-发布者,订阅者重构购物车

    发布者订阅者模式,是一种很常见的模式,比如: 一.买卖房子 生活中的买房,卖房,中介就构成了一个发布订阅者模式,买房的人,一般需要的是房源,价格,使用面积等信息,他充当了订阅者的角色 中介拿到卖主的房 ...

  3. [js高手之路]设计模式系列课程-组合模式+寄生组合继承实战新闻列表

    所谓组合模式,就是把一堆结构分解出来,组成在一起,现实中很多这样的例子,如: 1.肯德基套餐就是一种组合模式, 比如鸡腿堡套餐,一般是是由一个鸡腿堡,一包薯条,一杯可乐等组成的 2.组装的台式机同理, ...

  4. [js高手之路]设计模式系列课程-单例模式实现模态框

    什么是单例呢? 单,就是一个的意思.例:就是实例化出来的对象,那合在一起就是保证一个构造函数只能new出一个实例,为什么要学习单例模式呢?或者说单例模式有哪些常见的应用场景.它的使用还是很广泛,比如: ...

  5. [js高手之路]设计模式系列课程-设计一个模块化扩展功能(define)和使用(use)库

    模块化的诞生标志着javascript开发进入工业时代,近几年随着es6, require js( sea js ), node js崛起,特别是es6和node js自带模块加载功能,给大型程序开发 ...

  6. [js高手之路] 设计模式系列课程 - DOM迭代器(2)

    如果你对jquery比较熟悉的话,应该用过 eq, first, last, get, prev, next, siblings等过滤器和方法.本文,我们就用迭代设计模式来封装实现,类似的功能 < ...

  7. [js高手之路]设计模式系列课程-委托模式实战微博发布功能

    在实际开发中,经常需要为Dom元素绑定事件,如果页面上有4个li元素,点击对应的li,弹出对应的li内容,怎么做呢?是不是很简单? 大多数人的做法都是:获取元素,绑定事件 <ul> < ...

  8. [js高手之路] 设计模式系列课程 - 迭代器(1)

    迭代器是指通过一种形式依次遍历数组,对象,或者类数组结构中的每个元素. 常见的有jquery中的each方法, ES5自带的forEach方法. 下面我们就来自定义一个类似jquery或者ES5的迭代 ...

  9. [js高手之路] es6系列教程 - 迭代器与生成器详解

    什么是迭代器? 迭代器是一种特殊对象,这种对象具有以下特点: 1,所有对象都有一个next方法 2,每次调用next方法,都会返回一个对象,该对象包含两个属性,一个是value, 表示下一个将要返回的 ...

随机推荐

  1. JavaWeb 后端 <二> 之 Servlet 学习笔记

    一.Servlet概述 1.什么是Servlet Servlet是一个运行在服务器端的Java小程序,通过HTTP协议用于接收来自客户端请求,并发出响应. 2.Servlet中的方法 public v ...

  2. CJOJ 1071 【Uva】硬币问题(动态规划)

    CJOJ 1071 [Uva]硬币问题(动态规划) Description 有n种硬币,面值分别为v1, v2, ..., vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为 ...

  3. HDU 5527---Too Rich(贪心+搜索)

    题目链接 Problem Description You are a rich person, and you think your wallet is too heavy and full now. ...

  4. redhat6.4配置yum

    redhat6.4配置本地yum 1. 挂载(我喜欢放在/mnt下面)     mount -t auto *** /mnt/redhat     或     cp *** /mnt/redhat   ...

  5. 使用Lucene.net+盘古分词实现搜索查询

    这里我的的Demo的逻辑是这样的:首先我基本的数据是储存在Sql数据库中,然后我把我的必需的数据推送到MongoDB中,这样再去利用Lucene.net+盘古创建索引:其中为什么要这样把数据推送到Mo ...

  6. synchronized优化

    重量级锁 synchronized关键字 前文解释了synchronized的实现和运用,了解monitor的作用,但是由于monitor监视器锁的操作是基于操作系统的底层Mutex Lock实现的, ...

  7. word2vec原理(一) CBOW与Skip-Gram模型基础

    word2vec原理(一) CBOW与Skip-Gram模型基础 word2vec原理(二) 基于Hierarchical Softmax的模型 word2vec原理(三) 基于Negative Sa ...

  8. springboot启动时报错No session repository could be auto-configured.....

    具体异常提示: Error starting ApplicationContext. To display the auto-configuration report re-run your appl ...

  9. attr(),addClass()使用方法练习

    这次我主要是想要完成2个li之间样式的变化.方法比较傻,如果有人有更好的办法或者有别问题,希望可以不吝指教. <!DOCTYPE html><html> <head> ...

  10. Lettuce_webdriver 自动化测试

    这篇文章主要讲解以下几点: 1. Lettuce_webdriver环境搭建 2. lettuce_webdriver自动化实例讲解 一. lettuce_webdriver环境搭建 搭建lettuc ...