MySQL_Narb Posted July 6, 2012 Share Posted July 6, 2012 I've been looking for some good articles to explain this in-depth for a newbie to JavaScript. The only OOP language I've came from was PHP, and I'm fairly familiar with constructors there. But, what's the meaning of: function test(){ return true; } var o = new test(); Any reason why I don't just use this: function test(){ return true; } var o = test(); I've heard that using the new keyword is a bad habit, but I'd still like to understand the meaning of it. Also: What does the prototype keyword (I'm assuming that's the term for it?) mean? I have absolutely no clue on this one: Object.prototype Could anyone send me the link to an article explaining these? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/265325-meaning-of-objectprototype-and-the-new-keyword/ Share on other sites More sharing options...
Adam Posted July 7, 2012 Share Posted July 7, 2012 Prototype is used for extending objects. Every object in JS has prototype, and everything in JS is an object (even functions, strings, numbers, etc.) So essentially, you can use prototype to extend anything. There's a few pre-defined objects you're likely to be familiar with; String, Function, Number, Date, etc. It's also worth noting that unlike in PHP, objects only have properties. However you can assign those properties a Function, so it appears that they have methods, even though it's just a property containing a Function object. Also importantly, those objects - in-fact every object - extends Object. So to create a new, callable property for Strings, you would use: String.prototype.methodName = function() {} To create a new, callable property for every object, you would use: Object.prototype.methodName = function() {} These can only be used on constructed objects by the way, you couldn't call them directly like: String.methodName() It would have to be: 'foo'.methodName() The difference being that the methods are not added directly to the String object, they're added to instances of String objects as they're constructed -- that's prototype's job. Although if you chose you could drop "prototype." from the declaration and call it directly on String, like in my first call example. Making sense? You can also prototype your own, custom objects. For example: function Test() { } Test.prototype.returnFalse = function() { return false; } var test = new Test(); test.returnFalse(); // false Your calls to your example test() function don't do the same thing. The first, with the new keyword, creates a new instance of test() as an object, treating the function as a constructor. The second call just returns true and is stored in o. I'm not sure where you got it from that new is a bad habit? Behind the scenes you're always creating new objects in JS, it's perfectly fine if used right. Quote Link to comment https://forums.phpfreaks.com/topic/265325-meaning-of-objectprototype-and-the-new-keyword/#findComment-1359792 Share on other sites More sharing options...
MySQL_Narb Posted July 7, 2012 Author Share Posted July 7, 2012 Thank you Adam. I'm really glad you took your time to write that up for me. It definitely helped me get a better understanding of prototype and the new keyword. The only thing that I'm puzzled about, is that you showed: 'foo'.methodName(); What is this code representing? Shouldn't it be something like so: foo.prototype.method = methodName(); And are the quotes required? I'm assuming it just lets you define something on the spot. Quote Link to comment https://forums.phpfreaks.com/topic/265325-meaning-of-objectprototype-and-the-new-keyword/#findComment-1359877 Share on other sites More sharing options...
Adam Posted July 7, 2012 Share Posted July 7, 2012 No the code was a call to methodName(), equivalent to: var str = 'foo'; foo.methodName(); Given that a string is an object, you can still call methods on them even using the shorthand notation. JS provides a few other shorthand notations like that, for example: // Regex /^foo$/.test('bar') // Objects {foo: 'bar'} // Arrays ['foo', 'bar'] Quote Link to comment https://forums.phpfreaks.com/topic/265325-meaning-of-objectprototype-and-the-new-keyword/#findComment-1359881 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.