JavaScript: object and prototype, part0.1
Self-tutoring about JavaScript: the tutor continues about objects and the prototype connection.
It’s my understanding that in “old” JavaScript, you could have objects but they were defined using a constructor function, such as
function theObj(the_name){
this.name=the_name;
}
var myObj=new theObj(‘Smedley’);
The object above has no methods yet, but we could define one on its prototype (see my post here), as follows:
theObj.prototype.tellAboutMe=function(){
console.log(“My name is “+this.name+”.”);
};
Now, we can invoke the tellAboutMe method on myObj:
myObj.tellAboutMe();
Then we should receive the output My name is Smedley.
The word class didn’t reach JavaScript until 2015. However, the new keyword was always part of it. That’s how I understand it, anyhow.
Source:
Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.
Leave a Reply
You must be logged in to post a comment.