call method in javascript code example
Example 1: javascript function call with variable
function abc() {
alert('test');
}
var funcName = 'abc';
window[funcName]();
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: how to call a function in JavaScript
// calling our function
displayGreeting();
Example 4: calling function from function object javascript
var runApp = {
init: function(){
this.run()
},
run: function() {
alert("It's running!");
}
};
runApp.init();
Example 5: call function javascript
// Define your function
function sayHello(msg){
console.log("Hello, " + msg);
}
// Call it
sayHello("Norris");
// outputs:
// Hello, Norris
Example 6: how do i call a js method?
<button onclick="sayHello()">say hello</button> <script> 'use strict'; //force the context to be undefined function sayHello() { console.log(this); console.log(arguments); console.log('hello'); } </script>