I was experimenting with inheritance in javascript, and wrote those two functions:
Object.prototype.inherits=function(obj){this.prototype=new obj;}
Object.prototype.pass=function(obj){obj.prototype=new this;}
This code works very well:
Dog.inherits(Animal);
But the following fails:
Animal.pass(Dog);
As I understand it, my pass functions doesn't work, because "this" isn't a reference to the object instance itself? If that's the case, how can I reference the object from within itself?
Thanks in advance!
Well, actually the two are doing exactly the same:
Dog.prototype = new Animal;
The this
value inside the methods will refer to the base object where the reference was invoked, in the case of:
Dog.inherits(Animal);
The this
value will refer to the Dog
constructor function, and the obj
argument will be the Animal
function.
When you call:
Animal.pass(Dog);
The this
value will refer to the Animal
function, doing at the end exactly the same thing as the inherits
method, but the other way around.
I would recommend you to not extend the Object.prototype
object, because it can cause you a lot of problems, for example those two properties will be enumerated in any for-in
loop, eg:
for (var prop in {}) { // <-- an empty object!
alert(prop); // will alert 'inherits' and 'pass'
}
All objects inherit from Object.prototype
, and it seems that you intend to use those methods only on Function objects, it would be safer to extend the Function.prototype
object, or implement the methods as functions that take two parameters.
Works for me, with test code like:
function Animal() {}
Animal.prototype.isanimal= 1;
function Dog() {}
Animal.pass(Dog);
Dog.prototype.isdog= 2;
alert(new Dog().isanimal); // 1
alert(new Dog().isdog); // 2
However, bear in mind that new this
or new obj
will call the function this/obj
, creating a new full instance. If you have constructor code in that function that expects to receive some arguments, or sets up instance state, you can end up with problems. To create a new this
without calling this
as a function you can use a different constructor that does nothing:
function nonconstructor() {}
nonconstructor.prototype= this.prototype;
obj.prototype= new nonconstructor();
Also, you should avoid prototyping onto Object
. This will cause trouble for code using Object
as a general-purpose lookup map. As you only seem to be working with constructor-functions, prototyping onto Function
should meet your needs and is much safer.
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.