分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

jquery的优良继承方法

发布时间:2023-09-06 01:27责任编辑:白小东关键词:暂无标签

说一下好处:这个封装函数可以可以实现子类继承父类原型对象里面的所有方法和属性,但是也留了第二条路,去继承父类构造函数的里面的东西。

两个参数分别是子类的构造函数,后面是父类构造函数


$.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的优良继承方法

原文地址:http://www.cnblogs.com/guangixn/p/7895264.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved