jquery的优良继承方法
说一下好处:这个封装函数可以可以实现子类继承父类原型对象里面的所有方法和属性,但是也留了第二条路,去继承父类构造函数的里面的东西。
两个参数分别是子类的构造函数,后面是父类构造函数
$.inherits = function(childCtor, parentCtor) {
定以一个第三方构造函数
function tempCtor() {};
把父类的原型方法赋给第三方构造函数的原型对象
tempCtor.prototype = parentCtor.prototype;
这条的意思是先让子的构造函数,继承父构造函数的原型对象()
childCtor.superClass_ = parentCtor.prototype;,
子构造函数继承第三方构造函数的原型对象(跟上边一样,但是引用改变了)
childCtor.prototype = new tempCtor();
// childCtor.prototype.constructor = childCtor;
}
| <!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> | |
| </head> | |
| <body> | |
| </body> | |
| <script src="https://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script> | |
| <script src="./extend.js"></script> | |
| <script> | |
| function Parent(){ | |
| this.word = "hello" | |
| } | |
| // Parent.prototype = { | |
| // sayHello: function() { | |
| // alert(this.word) | |
| // } | |
| // } | |
| $.extend(Parent.prototype,{ | |
| sayHello: function() { | |
| alert(this.word) | |
| } | |
| }) | |
| function Child() { | |
| Parent.call(this) | |
| } | |
| // Child.prototype = { | |
| // sayHello: function() { | |
| // this.superClass_.sayHello(); | |
| // alert("world"); | |
| // } | |
| // } | |
| $.inherits(Child, Parent); | |
| $.extend(Child.prototype,{ | |
| sayHello: function() { | |
| Child.superClass_.sayHello.call(this); | |
| alert("world"); | |
| } | |
| }) | |
| var child = new Child() | |
| child.sayHello(); | |
| </script> | |
| </html> |
jquery的优良继承方法的更多相关文章
- jQuery源码06-jQuery = function(){};给JQ对象,添加一些方法和属性,extend : JQ的继承方法,jQuery.extend()
/*! * Includes Sizzle.js 选择器,独立的库 * http://sizzlejs.com/ */ (function( window, undefined ) { //" ...
- jquery编写插件的方法
版权声明:作者原创,转载请注明出处! 编写插件的两种方式: 1.类级别开发插件(1%) 2.对象级别开发(99%) 类级别的静态开发就是给jquery添加静态方法,三种方式 1.添加新的全局函数 2 ...
- jQuery源代码阅读之二——jQuery静态属性和方法
一.jQuery.extend/jQuery.fn.extend //可接受的参数类型如下:jQuery.extend([deep],target,object1,[objectN]) jQuery. ...
- 再谈:jquery编写插件的方法
版权声明:作者原创,转载请注明出处! 编写插件的两种方式: 1.类级别开发插件(1%) 2.对象级别开发(99%) 类级别的静态开发就是给jquery添加静态方法,三种方式 1.添加新的全局函数 2. ...
- jquery中的clone()方法使用
clone([Even[,deepEven]]) 描述: 克隆匹配的DOM元素并且选中这些克隆的副本. 在想把DOM文档中元素的副本添加到其他位置时这个函数非常有用. 1:一个布尔值(true 或者 ...
- jquery常用的一些方法
一.选择网页元素(标签选择器) $(document) //选择整个文档对象 $('#myId') //选择ID为myId的网页元素 $('div.myClass') // 选择class为myCla ...
- jquery中的ajax方法参数
引用来自:http://www.cnblogs.com/tylerdonet/p/3520862.html jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String ...
- [总结]jQuery之常用函数方法参考手册
w3school参考地址:http://www.w3school.com.cn/jquery/index.asp runoob参考地址:http://www.runoob.com/jquery/jqu ...
- jquery中的ajax方法参数总是记不住,这里记录一下。
1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如 ...
随机推荐
- 隐藏用户建立(Powershell)
最近做测试的时候发现,windows server2012 使用Mimikatz是直接抓不到明文密码的,而且,直接创建的账号登陆有时会碰到这个问题: ps:2012抓明文需要HKLM:\SYSTEM\ ...
- C++类中的Static关键字
静态成员是可以独立访问的,也就是说,无须创建任何对象实例就可以访问,而静态成员函数可不建立对象就可以被使用. 或者说静态函数与一般函数没有太大的区别,只是访问有限制,静态变量跟一般的全局变量的区别 ...
- CentOS7 64位安装mysql教程
参考链接:http://baijiahao.baidu.com/s?id=1597184796823517712&wfr=spider&for=pc https://www.cnblo ...
- style样式不换行
style="white-space:nowrap;"不自动换行<font color="" size=""></font ...
- 解决vue webApp使用lib-flexible和px2rem引用第三方ui库后,样式变小问题
首先,需要卸载项目中的postcss-px2rem. npm uninstall postcss-px2rem --save-dev 其次,安装postcss-px2rem-exclude npm i ...
- 2018-2019-2 《网络对抗技术》Exp4 恶意代码分析 Week6 20165311
2018-2019 20165311 网络对抗 Exp4 恶意代码分析 2018-2019 20165311 网络对抗 Exp4 恶意代码分析 实验内容 系统运行监控(2分) 恶意软件分析(1.5分) ...
- SQL语句之on子句过滤和where子句过滤区别
1.测试数据: SQL> select * from dept; DEPTNO DNAME LOC ------ -------------- ------------- ...
- python常用模块——collections
好久没学习了,简单了解下 Ⅰ.namedtuple 1.1 简单回顾一下tuple tuple是一个不可变的可迭代对象 ①可迭代 In [1]: test_tuple = (1,2,3,4,5) In ...
- 2018-2019-2 网络对抗技术 20165225 Exp6 信息搜集与漏洞扫描
2018-2019-2 网络对抗技术 20165225 Exp6 信息搜集与漏洞扫描 实践内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.O ...
- Python002-操作MSSQL(Microsoft sql server)基础示例(二)
前文http://www.cnblogs.com/fengpingfan/p/7675291.html,讲述了python操作mssql的步骤.环境创建.常用方法等,本文将实例演示python操作ms ...