Ramu26 Posted January 2, 2015 Share Posted January 2, 2015 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. Link to comment Share on other sites More sharing options...
davej Posted January 2, 2015 Share Posted January 2, 2015 Javascript is supposed to make sense? Maybe you have not yet watched the Doug Crockford lectures? https://www.youtube.com/watch?v=v2ifWcnQs6M&list=PL5586336C26BDB324 Link to comment Share on other sites More sharing options...
Ingolme Posted January 2, 2015 Share Posted January 2, 2015 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--;} Link to comment Share on other sites More sharing options...
Ramu26 Posted January 3, 2015 Author Share Posted January 3, 2015 Thanks Ingolme Link to comment Share on other sites More sharing options...
Hadien Posted January 3, 2015 Share Posted January 3, 2015 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. Link to comment Share on other sites More sharing options...
Ramu26 Posted January 3, 2015 Author Share Posted January 3, 2015 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 ? Link to comment Share on other sites More sharing options...
Ingolme Posted January 3, 2015 Share Posted January 3, 2015 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() { }}; Link to comment Share on other sites More sharing options...
justsomeguy Posted January 6, 2015 Share Posted January 6, 2015 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. Link to comment Share on other sites More sharing options...
Day Posted January 11, 2015 Share Posted January 11, 2015 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. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now