Jump to content

Recommended Posts

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.

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.

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.

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']

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.