Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I assign a function to the property of a Javascript object?

I was looking and struggling to the following example:

var player1= {name: "Chris", score: 1000, rank: 1};
var player2= {name: "Kristofer", score: 100000, rank: 2};

function playerDetails(){
alert("The name of the player is "+ this.name + "."+ " His score is : "+ this.score + "and his rank : "+ this.rank);
}
player1.logDetails= playerDetails;
player2.logDetails= playerDetails;

player1.logDetails();
player2.logDetails();

As far as I know player1.logDetails is a property of player1 or a method of player1. So I can't understand how the author assigns a property to a function. Also I don't get why you would write it like that instead of : player1.logDetails= playerDetails(); which I have tried and doesn't work.

Then he calls player1.logDetails() which is a function but not declared anywhere.(?)

If anyone could help?? Thank you in advance

like image 898
atomtm Avatar asked Dec 29 '13 14:12

atomtm


People also ask

Can a function be a property of an object JavaScript?

In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called.

Can you put a function in an object JavaScript?

You can call a function inside an object by declaring the function as a property on the object and invoking it, e.g. obj. sum(2, 2) . An object's property can point to a function, just like it can point to a string, number or other values. Copied!

How do you assign a function in JavaScript?

assign() which is used to copy the values and properties from one or more source objects to a target object. It invokes getters and setters since it uses both [[Get]] on the source and [[Set]] on the target. It returns the target object which has properties and values copied from the target object.

How do you associate function with object using JavaScript?

In JavaScript you can associate functions with objects. Here we will demonstrate how the constructor creates the object and assigns properties. Object is a standalone entity, with properties and type in JavaScript. JavaScript objects can have properties, that define their characteristics.


Video Answer


2 Answers

If the code was written like this, I bet you understand it:

var player1 = {
                name: "Chris", 
                score: 1000, 
                rank: 1,
                playerDetails: function() { alert('The name is '+ this.name) }
};

var player2 = {
                name: "Kristofer", 
                score: 10000, 
                rank: 2,
                playerDetails: function() { alert('The name is '+ this.name) }
};

The author of the code wanted to define the "playerDetails()" function once.

Another way of showing this in a simplified manner is:

var player1 = {
                name: "Chris", 
                score: 1000, 
                rank: 1
};

player1.playerDetails=function() { alert('The name is '+ this.name) }

var player2 = {
                name: "Kristofer", 
                score: 10000, 
                rank: 2
};

player2.playerDetails=function() { alert('The name is '+ this.name) }

So if you wanted to optimize the code above by only writing the playerDetails function once, it would look like the code in your post.

If I had written the code block, I might have written it like this: (which is easy to read)

function playerDetailsFunc() {alert('The name is '+ this.name) }

var player1 = {
                name: "Chris", 
                score: 1000, 
                rank: 1,
                playerDetails: playerDetailsFunc
};

var player2 = {
                name: "Kristofer", 
                score: 10000, 
                rank: 2,
                playerDetails: playerDetailsFunc
};
like image 112
Brian McGinity Avatar answered Oct 18 '22 13:10

Brian McGinity


Javascript functions are no different from other values or objects.
You can assign them to whatever you want; you can even pass them as parameters.

like image 45
SLaks Avatar answered Oct 18 '22 13:10

SLaks