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();
?
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.
A variable is something, which stores data. A function is a bunch of code, which can be executed, if you call.
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.
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.
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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With