.call in javascript code example
Example 1: running a function in a function javascript
function runFunction() {
myFunction();
}
function myFunction() {
alert("runFunction made me run");
}
runFunction();
Example 2: call js
function Product(name, price) {
this.name = name;
this.price = 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);
Example 3: call in javascript
function Person(name,place){
this.name = name;
this.place = place;
this.greet = function(){
return (`HEllo I am ${this.name} comes from ${this.place}`);
}
};
function Teacher(name,place,sub){
this.sub = sub;
Person.call(this , name , place);
this.greeting = function(){
return(`I am ${name}, from ${place} and I teach ${this.sub}`);
}
}
var Te1 = new Teacher("Ranjan" , "Salem" , "JavaScript");
Te1
Te1.name
Te1.place
Te1.greeting()
function Student(name,place,like){
this.like = like;
Person.apply(this , [name , place]);
this.about_me = function(){
return(`My name is ${name}, I come from ${place} and I
like to play${this.like}`);
}
}
var Pe1 = new Student("Max" , "Mettur" , "Cricket");
Pe1
Pe1.name
Pe1.place
Pe1.about_me()
Example 4: call function javascript
function sayHello(msg){
console.log("Hello, " + msg);
}
sayHello("Norris");