Saryk
Saryk

Reputation: 355

Defining a member function of an object

I declare the following object :

var Node = {
  nodeID: '',
  parentID: '',
  nodeName: '',
  children: []
}

and I'd like to write a Contains() function to check if the children array contains a specific Node object, used like Node.Contains(otherNode).

Where and how should I declare it to be able to use this syntax ?

Upvotes: 0

Views: 193

Answers (1)

hsz
hsz

Reputation: 152216

Just declare this function next to the other properties:

var Node = {
  nodeID: '',
  parentID: '',
  nodeName: '',
  children: [],
  Contains: function(otherNode) {
    return this.children.indexOf(otherNode) > -1;
  }
}

Upvotes: 1

Related Questions