Javascript call
couple of examples...
by peterbenoit
JavaScript
var animals = [{
species: 'Lion',
name: 'King'
}, {
species: 'Whale',
name: 'Tail'
}];
for (var i = 0; i < animals.length; i++) {
(function (i) {
this.print = function () {
console.log('#' + i + ' ' + this.species + ': ' + this.name);
}
}).call(animals[i], i);
}
animals[0].print();
////========================
function Product(name, price) {
this.name = name;
this.price = price;
this.print = function(){
console.log(name + " is $" + price);
};
if (price < 0) throw RangeError('Cannot create product "' + name + '" with a negative price');
return this;
}
function Food(name, price) {
Product.call(this, name, price);
this.category = 'food';
}
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);
fun.print();