ECMAScript 继承机制实现问题

半夏情 2015-12-25 10:50:43
当我使用混合方式定义“类”ClassA时;使用对象冒充方式ClassB继承ClassA,为何报错原型函数未定义;
function ClassA(sColor){
this.color = sColor;
/*this.sayColor = function(){
console.log(this.color);
}*/ //这种构造方式是可以的
}

//原型方法
ClassA.prototype.sayColor = function(){
console.log(this.color);
}

function ClassB(sColor,sName){
this.newMethod = ClassA;//函数名只是指向它的指针
this.newMethod(sColor);//调用(复制);
delete this.newMethod;
//所有新属性和新方法都必须在删除了新方法的代码行后定义。否则,可能会覆盖超类的相关属性和方法:
this.name = sName;
}
ClassB.prototype.sayName = function(){
console.log(this.name);
}
var objA = new ClassA("red");
objA.sayColor();
var objB = new ClassB("blue","Bill");
objB.sayName();
objB.sayColor();//这里有问题
报错如下:
objB.sayColor is not a function
at Object.
...全文
40 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
街头小贩 2015-12-25
  • 打赏
  • 举报
回复
以我学的知识看不出它有不报错的理由
顾小林 2015-12-25
  • 打赏
  • 举报
回复
goog.base = function(me, opt_methodName, var_args) { var caller = arguments.callee.caller; if (goog.STRICT_MODE_COMPATIBLE || (goog.DEBUG && !caller)) { throw Error('arguments.caller not defined. goog.base() cannot be used ' + 'with strict mode code. See ' + 'http://www.ecma-international.org/ecma-262/5.1/#sec-C'); } if (caller.superClass_) { // Copying using loop to avoid deop due to passing arguments object to // function. This is faster in many JS engines as of late 2014. var ctorArgs = new Array(arguments.length - 1); for (var i = 1; i < arguments.length; i++) { ctorArgs[i - 1] = arguments[i]; } // This is a constructor. Call the superclass constructor. return caller.superClass_.constructor.apply(me, ctorArgs); } // Copying using loop to avoid deop due to passing arguments object to // function. This is faster in many JS engines as of late 2014. var args = new Array(arguments.length - 2); for (var i = 2; i < arguments.length; i++) { args[i - 2] = arguments[i]; } var foundCaller = false; for (var ctor = me.constructor; ctor; ctor = ctor.superClass_ && ctor.superClass_.constructor) { if (ctor.prototype[opt_methodName] === caller) { foundCaller = true; } else if (foundCaller) { return ctor.prototype[opt_methodName].apply(me, args); } } // If we did not find the caller in the prototype chain, then one of two // things happened: // 1) The caller is an instance method. // 2) This method was not called by the right caller. if (me[opt_methodName] === caller) { return me.constructor.prototype[opt_methodName].apply(me, args); } else { throw Error( 'goog.base called from a method of one name ' + 'to a method of a different name'); } }; 使用方法 var A = funciton(option) { //xxxx } var B = function(option) { goog.base(this, option); } goog.inherits(A,B);
顾小林 2015-12-25
  • 打赏
  • 举报
回复
假装这么理解 // 以下都是讲义代码 非正式代码 function ClassA(sColor){ this = new Object(); this.color = sColor; /*this.sayColor = function(){ console.log(this.color); }*/ //这种构造方式是可以的 } //原型方法 ClassA.prototype.sayColor = function(){ console.log(this.color); } function ClassB(sColor,sName){ this = new Object(); this.newMethod = ClassA; this.newMethod(sColor);// 你这一句的确是new了个classA 但是 你并没有赋值 此时this与classA并没有关系 //this = this.newMethod(sColor) //加上这这句你才集成了ClassA // delete this.newMethod; //所有新属性和新方法都必须在删除了新方法的代码行后定义。否则,可能会覆盖超类的相关属性和方法: this.name = sName; } 然后看一下大部分高手写的继承代码 goog.inherits = function(childCtor, parentCtor) { /** @constructor */ function tempCtor() {}; tempCtor.prototype = parentCtor.prototype; childCtor.superClass_ = parentCtor.prototype; childCtor.prototype = new tempCtor(); /** @override */ childCtor.prototype.constructor = childCtor; /** * Calls superclass constructor/method. * * This function is only available if you use goog.inherits to * express inheritance relationships between classes. * * NOTE: This is a replacement for goog.base and for superClass_ * property defined in childCtor. * * @param {!Object} me Should always be "this". * @param {string} methodName The method name to call. Calling * superclass constructor can be done with the special string * 'constructor'. * @param {...*} var_args The arguments to pass to superclass * method/constructor. * @return {*} The return value of the superclass method/constructor. */ childCtor.base = function(me, methodName, var_args) { // Copying using loop to avoid deop due to passing arguments object to // function. This is faster in many JS engines as of late 2014. var args = new Array(arguments.length - 2); for (var i = 2; i < arguments.length; i++) { args[i - 2] = arguments[i]; } return parentCtor.prototype[methodName].apply(me, args); }; };

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧