In JavaScript: the difference in syntax between the definition of a function and a method in a class

The Object class has both methods and functions, meaning that both of them are accessible through Object.nameOfMethodOrFunction (). The next question What is the difference between a method and a function explains the difference between a method and a function, but does not explain how to create them within an object. For example, the code below defines the sayHi method. But how do you define a function inside the same object?

var johnDoe = { fName : 'John', lName: 'Doe', sayHi: function() { return 'Hi There'; } }; 
+6
source share
3 answers

Two classes are defined below: ClassA and ClassB , which have equal functionality but are 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 are not much different in use, and they are both “methods”.

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

So, what do you mean by “function”, according to the msdn link you gave as a comment, it seems that the “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.

+7
source
 var johnDoe = { fName: 'John', lName: 'Doe', sayHi: function(){ function message(){ return 'Hi there'; } return message(); } }; 

This is as good as you are going to get using the object declaration method to create a "class" in JavaScript. Just keep in mind that the function is only valid in the sayHi .

However, if you use a function as a class structure, you will have a bit more flexibility:

 var johnDoe = function(){ this.publicFunction = function(){ }; var privateFunction = function(){ }; }; var jd = new johnDoe(); jd.publicFunction(); // accessible jd.privateFunction(); // inaccessible 

(although both of them are really considered methods, since they have access to the object area inside).

0
source

Quoting Aaron using "The method is on the object. The function is independent of the object."

Logically, the method is useless without "this".

Consider the following example:

 var johnDoe = { fName: 'John', lName: 'Doe', sayHi: function () { return 'Hi There, my name is ' + this.fName; } }; function sayHi2() { return 'Hi There, my last name is ' + this.lName; } //Will print Hi there, my first name is John alert(johnDoe.sayHi()); //An undefined will be seen since there is no defined "this" in SayHi2. alert(sayHi2()); //Call it properly now, using the oject johnDoe for the "this" //Will print Hi there, my last name is Doe. alert(sayHi2.call(johnDoe)); 
0
source

All Articles