0

I'm making a LinkedList class:

function LinkedList(){
           ...

What is the difference between:

this.addNode = function(data){
           ...

and

addNode: function(data){
           ...
ionox0
  • 1,101
  • 2
  • 14
  • 21
  • possible duplicate of [What does ':' (colon) do in JavaScript?](http://stackoverflow.com/questions/418799/what-does-colon-do-in-javascript) – idmadj Mar 30 '14 at 21:58
  • 2
    https://blog.jcoglan.com/2007/07/23/writing-a-linked-list-in-javascript/ lol are you doing homework? – ncubica Mar 30 '14 at 22:05

1 Answers1

2

This creates a property on an object (assuming this references an object)

this.addNode = function(data) { ...

That's an assignment expression, and if it's all by itself then it comprises a statement.

This, on the other hand, is part of an object literal expression:

addNode: function(data) { ...

It only makes sense inside an object literal, which looks like:

var someObject = {
  property1: value1,
  property2: value2,
  // ...
};

In a larger sense, it doesn't make a lot of sense to compare the two; they're two different ways of doing the same thing, in a way, but they make sense in disjoint contexts. The first is a way of adding or resetting a property on an existing object, while the second is a way of setting a property as part of the creation of a new object.

Pointy
  • 405,095
  • 59
  • 585
  • 614