I have this Demo which execution alert and it is work well:
below the code.
var Shape = function(){};
var TwoDShape = function(){};
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){return this.name;};
alert('there is alert');
when I add this line : extend(TwoDShape, Shape);
I can't to execution alert as you can see Demo
after this I add lines:
var my = new TwoDShape();
alert(my.toString());
alert(TwoDShape.prototype.name);
alert(my.hasOwnProperty('name'));
to alert the name of the class shape
or TwoDShape
but I can't successfull to display the class.name why ?
First, you do not have function extend()
inside you code, so the code encounters error there and stopped running.
So I fixed it using jQuery $.extend()
, or you can write the extend()
yourself (see Jimmy's answer).
Secondly, if you want all Shape/TwoDShape instances have the class name
to be accessible using toString()
, you need to extend the prototype:
TwoDShape.prototype = $.extend(TwoDShape.prototype, Shape.prototype);
See the jsfiddle I created http://jsfiddle.net/4Bjjp/6/ on extending prototype
Your code break because extend
function does not exist. You've to implement your own to make it work. Here is a simple implementation.
function extend(destination, source) {
for (var key in source) {
if(source.hasOwnProperty(key)) {
destination[key] = source[key];
}
}
return destination;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.