bdd_book icon indicating copy to clipboard operation
bdd_book copied to clipboard

Question in / javascript_refresher / product_method_lookup.js

Open jryu01 opened this issue 10 years ago • 2 comments

Reference: https://github.com/marcoemrich/bdd_book/blob/master/javascript_refresher/product_method_lookup.js#L15-L25

var Book = Object.create(Product).extend({
    init: function(name, author) {
        Product.init(name);
        this._author = author;
        return this;
    },

    _author: null,
    setAuthor: function(author) { this._author = author; },
    author: function() { return this.author(); }
});

From above code, can you explain why you called Product.init(name) in init of the Book prototype? With above, if I create two instances of book with different name,

var myBook = Object.create(Book).init('Lords of the Rings', 'J.R.R. Tolkien'); var myBook2 = Object.create(Book).init('The Hobbit', 'J.R.R. Tolkien');

The myBook._name is overwritten by name initialized in myBook2 and myBook.name() also returns 'The Hobbit'

I don't think it is what you intended for. and how do I properly inherit init function from product prototype?

jryu01 avatar Sep 17 '14 00:09 jryu01

Hi Jaehwan,

sorry for my late answer. I'm still stuck in a heap of work.

And thanks a lot for your feedback. Actually, I'm already aware of two errors you described. There is also a fix for the inheritance-Problem. All that is missing is a bind to change the "this" for the Book. I attached you the errata-file.

Feel free to ask, if you have any further questions or suggestions.

Marco

On Wed, Sep 17, 2014 at 2:03 AM, Jaehwan Ryu [email protected] wrote:

Reference:

https://github.com/marcoemrich/bdd_book/blob/master/javascript_refresher/product_method_lookup.js#L15-L25

var Book = Object.create(Product).extend({ init: function(name, author) { Product.init(name); this._author = author; return this; },

_author: null, setAuthor: function(author) { this._author = author; }, author: function() { return this.author(); }

});

From above code, can you explain why you called Product.init(name) in init of the Book prototype? With above, if I create two instances of book with different name,

var myBook = Object.create(Book).init('Lords of the Rings', 'J.R.R. Tolkien'); var myBook2 = Object.create(Book).init('The Hobbit', 'J.R.R. Tolkien');

The myBook._name is overwritten by name initialized in myBook2 and myBook.name() also returns 'The Hobbit'

I don't think it is what you intended for. and how do I properly inherit init function from product prototype?

— Reply to this email directly or view it on GitHub https://github.com/marcoemrich/bdd_book/issues/3.

http://pastebin.com/2TMD8yFu http://pastebin.com/WwpSmpcG

/*

Regarding code in book: BDD with JS -- Listing 1.17

PROBLEM: myBook objects are sharing book name, see http://pastebin.com/2TMD8yFu

Answer:

The problem is in line 39;

Product.init(name);

This calls the right init-Method, but with the wrong this-object.

The solution is to rebind this:

(Product.init.bind(this))(name);

Complete code below.

Thanks for finding this Bug Frank - I will include it in the book errata.

*/

var Book, Product, myBook1, myBook2;

Object.prototype.extend = function(props) { var prop; for (prop in props) { this[prop] = props[prop]; } return this; };

Product = { _name: "", init: function(name) { this._name = name; return this; }, name: function() { return this._name; }, setName: function(name) { return this._name = name; } };

Book = Object.create(Product).extend({ _author: null, init: function(name, author) { (Product.init.bind(this))(name); this._author = author; return this; }, setAuthor: function(author) { return this._author = author; }, author: function() { return this._author; } });

myBook1 = Object.create(Book).init('Lords of the Ring', 'J.R.R. Tolkien');

myBook1.mostImportantHobbit = "Frodo";

myBook2 = Object.create(Book).init('BDD with JS', 'Frank');

myBook2.mostImportantHobbit = "Bento";

console.log(myBook1.name());

console.log(myBook1.author());

console.log(myBook1.mostImportantHobbit);

console.log(myBook2.name());

console.log(myBook2.author());

console.log(myBook2.mostImportantHobbit);

/*

OUTPUT

Lords of the Ring J.R.R. Tolkien Frodo BDD with JS Frank Bento

*/

marcoemrich avatar Sep 29 '14 09:09 marcoemrich

Thanks! Your book is really awesome. It would be really awesome and I will be happy to read if you have future release covering more advanced topics with more real word problems!! Currently, I find It's quite hard to find enough good materials on BDD in javascript :(

jryu01 avatar Sep 29 '14 16:09 jryu01