I play with following JS code. And I have 2 questions.
1) Why User is not prototype of author_1?
2) Why after the resetting of Author.prototype author_1 becomes not an instanceof Author?
function User(_fname){
this.fname = _fname;
return this;
}
function Author(){
this.book = "Magick of JS";
return this;
}
Author.prototype = new User('John');
author_1 = new Author;
console.log("=======================");
console.log(author_1 instanceof Author); // true
console.log(author_1 instanceof User); // true
console.log(User.isPrototypeOf(author_1)); // false (>>>> 1) WHY? <<<<)
console.log(author_1.constructor); // User(_fname)
console.log(author_1.__proto__); // User { fname="John"}
console.log(Object.getPrototypeOf(author_1)); // User { fname="John"}
console.log(author_1.constructor.prototype); // User {}
Author.prototype = new User('Alex');
author_2 = new Author;
console.log("=======================");
console.log(author_1 instanceof Author); // false (>>>> 2) WHY? <<<<)
console.log(author_1 instanceof User); // true
console.log(User.isPrototypeOf(author_1)); // false
console.log(author_1.constructor); // User(_fname)
console.log(author_1.__proto__); // User { fname="John"}
console.log(Object.getPrototypeOf(author_1)); // User { fname="John"}
console.log(author_1.constructor.prototype); // User {}
console.log("=======================");
console.log(author_2 instanceof Author); // true
console.log(author_2 instanceof User); // true
console.log(User.isPrototypeOf(author_2)); // false
console.log(author_2.constructor); // User(_fname)
console.log(author_2.__proto__); // User { fname="Alex"}
console.log(Object.getPrototypeOf(author_2)); // User { fname="John"}
console.log(author_2.constructor.prototype); // User {}
console.log("=======================");
console.log(author_1); // User {book: "Magick of JS", fname: "John"}
console.log(author_2); // User {book: "Magick of JS", fname: "Alex"}
Thanks!
UPDATE
Thanks for help! But now I can't understand how author_1 can know that it's an Author
function log(){ console.log.apply(console, arguments) }
function User(_fname){
this.fname = _fname;
return this;
}
function Author(){
this.book = "Magick of JS";
return this;
}
Author.prototype = new User('John');
author_1 = new Author;
log(author_1); // User { book="Magick of JS", fname="John"}
log(author_1.__proto__); // User { fname="John"}
log(author_1.constructor); // User(_fname)
log(author_1 instanceof Author); // true
// How author_1 kowns that it's an Author? Where is property?
// Can I find it in web inspector? Or it's hidden value?
()afternew Authorfor both author_1 and author_2? That may vastly change your results.new ConstructorFunctionbehaves exactly the same asnew ConstructorFunction(). It's just less common syntax.return thisinside the functions acting like constructors?