by phpnode · 12y ago · 0 comments
var Person = User.extend({});
var person = Person();
person instanceof Person; // false!
person instanceof User; // truePersonally, I only use this pattern for cases where it's more convenient not to have to use new (e.g. building declarative APIs for defining things) and let 'use strict'; catch the rest.
if (!(this instanceof User)) {
return new User(name, lastname);
}
I'm pointing out that this doesn't work if you extend the class.The point is - don't forget `new` because tricks like this will not save you, in fact this is arguably worse - you think you're getting a `Person` but you get a `User` with no immediate errors, at least if you forget `new` then you'll get an error straight away in strict mode.
For now, I'll keep using `_.create` to do Inheritance. Of course it means another library to keep using and more verbose. But whatever...
var Person = function(){ User.call(this) };
Person.prototype = _.create(User.prototype, { 'constructor': Person });
var p = new Person();
p instanceof User //true
p instanceof Person //true