oop - JavaScript Prototype property stealing / inheritance -
is correct way prototype inheritance , property 'stealing??'/inheritance. want inherit properties person constructor + methods.
function product(name, price) { this.name = name; this.price = price; } product.prototype.tellme = function() { console.log('name = ' + this.name + ', price = ' + this.price); } function food(name, price, category) { product.call(this, name, price); this.category = category; } food.prototype = object.create(product.prototype); food.prototype.constructor = food; var b = new food('name', 123, 'category'); console.log(b); b.tellme(); var = new product('name2', 321); console.log(a);
i appreciate if give me example. thanks!
latest javascript standard, ecma6, enables perform inheritance writing class in similar fashion java oop. developer highly familiar java oop, have found approach easy understand , adapt. @ attached snippet code rewritten. hope helps.
'use strict'; class product{ constructor(name,price){ this.name = name; this.price = price; } tellme(){ console.log('name = ' + this.name + ', price = ' + this.price); } } class food extends product{ constructor(name, price, category){ super(name, price); this.category = category; } } var b = new food('name', 123, 'category'); console.log(b); b.tellme(); var = new product('name2', 321); console.log(a);
Comments
Post a Comment