Jump to content
Sign in to follow this  
Ramu26

Objects without classes in JS

Recommended Posts

Hi All,

 

Why there are no classes in JS ? if there are no classes then how are we creating function objects

examplefunction Person(){}var p = new Person(); we are instantiating a function instead of class, how can this happen and how is instanceof check passing if there are no classes ?If there were their then data and accessor properties could have been applied only once instead of defining them every time when the Object is created right?for example:var o={};Object.defineProperty(o,name,{ value : "testingNameProperty", writeable: false});if had class we could have configured this directly on class instead of object "o", which makes more sense,In this case, we have to define this every time the object is created.

Share this post


Link to post
Share on other sites

For some odd reason, functions are able to behave like classes in Javascript. This is helpful. I'm not entirely sure of the reasoning behind implementing it this way, but all browsers do it like that so developers take advantage of it.

 

If you use a function instead of the Object() methods you're capable of giving the class a name, private properties, public properties and methods.

function MyClass() {    // The methods of this class might be called in another context which would cause "this" to point to something else    // So we assign a reference to this object in a new variable    var self = this;    // Private property    var privateProperty1 = 0;    var privateProperty2;    // Public property    this.publicProperty1 = 5;    // Private method    function privateMethod1() {        privateProperty1++;    }    // Privileged method (has access to private properties and methods)    this.privilegedMethod = function() {        self.publicProperty1++;        privateProperty2 = "A";    }}// Public method (does not have access to private variables and methods)MyClass.prototype.publicMethod = function() {    this.publicProperty--;}

Share this post


Link to post
Share on other sites

Everything in Javascript are objects...objects with properties. even the functions are simple objects

 

To get purely technical, Javascript doesn't have classes just objects that can "fake" the behavior of classes, at least enough to get by. instanceof is really just finding if one instance points to the prototype object its requested to compare to, its not making any real type-based comparison as in other languages. Over the years the javascript language itself has been slightly motified and upgraded and there is more power to making "classes", but at its roots you still don't make any "true" class.

Share this post


Link to post
Share on other sites

Correct, but, My question how can Objects be created without classes. That eventually means, the language internal has class, but, not exposed as language feature right ?

Share this post


Link to post
Share on other sites

Ordinary objects are created using JSON syntax, or the old way, using the new Object() constructor.

// Old way:var obj = new Object();obj.property = "something";obj.method = function() {}// JSONvar obj = {    property : "Something",    method : function() { }};

Share this post


Link to post
Share on other sites

The term you're looking for is prototype. Javascript uses prototypes, not classes. There is a String prototype, for example. If you add a new property or method to the String prototype then all string object instances have that property or method.

Share this post


Link to post
Share on other sites

I'm glad my curiosity led me to open up this thread. Hadien and justsomeguy, you made something that seemed so complicated very simple and easy to understand.

 

Thank you.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...