Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JavaScript: Syntax difference between function & method definition within a class

The Object class has both methods and functions meaning they both are accessed through Object.nameOfMethodOrFunction(). The following question What is the difference between a method and a function explains the difference between a method and and a function, but it doesn't explain how to create them within an object. For example, the code below defines the method sayHi. But how do you define a function inside the same object?

var johnDoe =
{
      fName : 'John',
      lName: 'Doe',
      sayHi: function()
      {
        return 'Hi There';
      }
};
like image 751
user1888243 Avatar asked Dec 30 '12 06:12

user1888243


People also ask

What is the difference between function and function in JavaScript?

function is a language keyword used to define functions. Function is the builtin prototype object that represents all functions. Save this answer.

How are arrow functions () => {} different than traditional function expressions?

In regular function, arguments will give you list of parameter passed in function, In arrow function arguments is not defined. In regular function, you always have to return any value, but in Arrow function you can skip return keyword and write in single line. In arrow function parameters should be unique.

What are the 3 types of functions in JavaScript?

There are 3 ways of writing a function in JavaScript: Function Declaration. Function Expression. Arrow Function.

What is the difference between function and class in JavaScript?

One key distinction between functions and classes was highlighted in this talk which suggests that a function is a behavior that can carry data while, inversely, a class is data that can carry behavior.


1 Answers

The following defines two classes, ClassA and ClassB, with equal functionality but different in nature:

function ClassA(name){
    this.name = name;
    // Defines method ClassA.say in a particular instance of ClassA
    this.say = function(){
        return "Hi, I am " + this.name;
    }
}

function ClassB(name){
    this.name = name;
}
// Defines method ClassB.say in the prototype of ClassB
ClassB.prototype.say = function(){
    return "Hi, I am " + this.name;
}

As shown below, they doesn't differ much in usage, and they are both "methods".

var a = new ClassA("Alex");
alert(a.say());
var b = new ClassB("John");
alert(b.say());

So now what you mean for "function", according to the msdn link that you gave as a comment, seems that "function" is just a "static method" like in C# or Java?

// So here is a "static method", or "function"?
ClassA.createWithRandomName = function(){
    return new ClassA("RandomName"); // Obviously not random, but just pretend it is.
}

var a2 = ClassA.createWithRandomName(); // Calling a "function"?
alert(a2.say()); // OK here we are still calling a method.

So this is what you have in your question:

var johnDoe =
{
      fName : 'John',
      lName: 'Doe',
      sayHi: function()
      {
        return 'Hi There';
      }
};

OK, this is an Object, but obviously not a class.

like image 92
Alvin Wong Avatar answered Nov 02 '22 12:11

Alvin Wong