Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
GreenGeeks Web Hosting - Faster, Scalable & Eco-Friendly

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,930 questions

11,045 answers

573 users

How to use apply in JavaScript

1 Answer

0 votes
var worker1 = {name: 'Holly', age: 33, from: 'NY'};
var worker2 = {name: 'Molly', age: 31, from: 'LA'};

var workerDetails = function()
{
    document.write(this.name + ' ' + this.age + ' ' + this.from  + '<br />');
};

var workerSalary = function(salary)
{
    document.write(this.name + ' salary: $' + salary + '<br />');
};

var updateDetails = function(name, age, from)
{
    this.name = name;
    this.age = age;
    this.from = from;
};


var run = function(worker, method, args)
{
    method.apply(worker, args);
};


run(worker1, workerDetails);
run(worker1, workerSalary, [13873]);

run(worker1, updateDetails, ['Holly', 33, 'CA']);
run(worker1, workerDetails);


/*
run:
  
Holly 33 NY
Holly salary: $13873
Holly 33 CA
    
*/

 




answered Jul 27, 2015 by avibootz
...