Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding JavaScript in detail: is it a class, a function, or just a variable?

I am a beginner in JavaScript, and I am finding one concept very confusing. Consider the code below:

var person = {      firstName   :"Penelope",      lastName    :"Barrymore",      // Since the "this" keyword is used inside the showFullName method below, and the showFullName method is defined on the person object,​      // "this" will have the value of the person object because the person object will invoke showFullName ()​      showFullName:function () {          console.log (this.firstName + " " + this.lastName);      } ​  } ​  person.showFullName (); // Penelope Barrymore 

Is person a class or function or just a variable?

If assuming that person is a class, is the code person.showFullName (); the right way of invoking it, because in C# or anyother language we write

person perObj = new person(); perObj.showFullName(); 

?

like image 996
user5283721 Avatar asked Jan 01 '16 11:01

user5283721


People also ask

Is a JavaScript method a function?

JavaScript methods are actions that can be performed on objects. A JavaScript method is a property containing a function definition. Methods are functions stored as object properties.

What is the difference between JavaScript function and variables?

A variable is something, which stores data. A function is a bunch of code, which can be executed, if you call.

What is class in JavaScript definition?

Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes but also have some syntax and semantics that are not shared with ES5 class-like semantics.

Is JavaScript function a class?

Classes Are FunctionsA JavaScript class is a type of function. Classes are declared with the class keyword. We will use function expression syntax to initialize a function and class expression syntax to initialize a class. We can access the [[Prototype]] of an object using the Object.


2 Answers

person is an object. It has 3 properties, named firstName, lastName, and showFullName. The first two properties contain strings. The last property contains a function.

When you call a function with the syntax <expression>.<function>(<arguments>), where <expression> evaluates to an object and <function> is the name of one of its properties, then while the function is running the special variable this is set to the object. That's how this.firstName and this.lastName are able to access those properties of the object.

This feature is not very useful when there's just a single object, since it could easily just use the person variable. But you could use the same function for multiple objects.

function showFull() {     console.log(this.firstName + " " + this.lastName); } var person1 = {     firstName: "Penelope",     lastName: "Barrymore",     showFullName: showFull }; var person2 = {     firstName: "John",     lastName: "Smith",     showFullName: showFull } person1.showFullName(); // Penelope Barrymore person2.showFullName(); // John Smith 
like image 116
Barmar Avatar answered Oct 04 '22 04:10

Barmar


Just to add to Barmar, you could have done something like this as well (in case you find it more similar to C#):

var person = function() {      this.firstName = "";      this.lastName = ""; }   person.prototype.showFullName = function () {       console.log (this.firstName + " " + this.lastName);  }   var perObj = new person(); perObj.firstName = "Penelope"; perObj.lastName = "Barrymore"; perObj.showFullName(); 
like image 20
loxxy Avatar answered Oct 04 '22 05:10

loxxy