/*继承*/ function ClassA(obj){ this.color = obj; this.b = "TK"; this.sayColor = function(){ console.log(this.color); } this.x = function(){ console.log("全部调用了?"+this.b); } } /*继承ClassA的方法1 是将其作为常规函数,然后调用者的引用指向该常规函数*/ function ClassB(obj){ this.newMethod = ClassA; //函数调用 this.newMethod(obj); delete this.newMethod; /*es中引入了call()方法 对继承进行了封装*/ //ClassA.call(this,obj); this.name = obj; this.sayName = function(){ console.log(this.name); } } /*原型链*/ function ClassF(){ } ClassF.prototype.attrOne = 'protest'; ClassF.prototype.attrMethod = function(){ console.log(this.attrOne); } function ClassS(){ } ClassS.prototype = new ClassF(); ClassS.prototype.myAttr = ''; ClassS.prototype.myMethod = function(){ console.log(this.myAttr); } /*混合模式联系 -- 对象冒充继承构造函数属性 原型继承构造函数方法*/ function fTree(obj){ this.name = obj; this.fMethod = function(){ console.log(this.name); } } function subTree(ownAttr,obj){ /*对象冒充继承父类属性*/ fTree.call(this,obj); this.ownAttr = ownAttr; } /*使用原型继承父类方法*/ subTree.prototype = new fTree(); subTree.prototype.ownMethod = function(){ console.log(this.ownAttr); } /*回调函数理解*/ function outerFunc(obj,callbackFunc1,callbackFunc2){ console.log(111111111111); callbackFunc1(); callbackFunc2("函数调用2.。。。。。。。。。。。。"); } function createServer(callbackFunc){ console.log("开始创建服务器....."); function req(){ console.log("请求信息............"); } function res(){ console.log("响应信息............"); } callbackFunc(req,res); } function loopClose(obj,param1,callFunc){ callFunc(obj); } /*闭包再次理解*/ function generateClosure(){ var count = 0; var get = function(){ count++; return count; } return get; } /*递归调用*/ function recursiveFunc(num) { if(num <= 1) { return 1; } else { //console.log(arguments); return num * arguments.callee(num-1); } } console.log(recursiveFunc.toString());
javascript深入原型
最新推荐文章于 2024-06-27 10:11:54 发布