I am a bit curious about javascript prototype I found sample in here and I made little modification so I can try it like this:
<html>
<body>
<script>
function Product(name, price) {
this.name = name;
this.price = price;
}
function Food(name, price) {
Product.call(this, name, price);
this.category = 'food';
this.otherName = name;
}
Food.prototype = new Product();
function Toy(name, price) {
Product.call(this, name, price);
this.category = 'toy';
}
Toy.prototype = new Product();
var cheese = new Food('feta', 5);
var fun = new Toy('robot', 40);
var prod = new Product('test', 20);
console.log(cheese);
console.log(fun);
console.log(prod);
</script>
</body>
</html>
and it returns like this
cheese = Food {name: "feta", price: 5, category: "food", otherName: "feta", name: undefined, price: undefined}
fun = Toy {name: "robot", price: 40, category: "toy", name: undefined, price: undefined}
prod = Product {name: "test", price: 20}
its make property name
and price
twice, is it more efficient if we distinguish Food.prototype = new Product();
and Toy.prototype = new Product();
Why must I use that line?