1

Can anyone tell me if the following example classes are equivalent, with respect to the class properties. One is defined within the constuctor and one outside.

Example 1

function Test(input)
{
    this.output = input ;
    Test.WELCOME = "Hello ;
}

Example 2

function Test(input)
{
    this.output = input ;        
}

Test.WELCOME = "Hello ;

I have been using the second type but both seems to work.

Cheers

4
  • Related: Class keyword in Javascript. Zoso - Where is this JavaScript running? (probably not a browser) Commented Nov 30, 2011 at 11:28
  • unless you are in some very early ES.Next alpha engine, there is no used class keyword in javascript. Commented Nov 30, 2011 at 11:28
  • sorry mistyped class should have been function. These are simulated classes not true class. Commented Nov 30, 2011 at 11:32
  • 1
    @Kobi Ahh, interesting! Cool. Commented Nov 30, 2011 at 11:35

2 Answers 2

4

I'm pretty sure neither of those does what you really want it to do. The usual way this inside of Test is something useful will be when instantiating it using new. Setting a property on Test itself doesn't have any effect on objects created from it using x = new Test('someinput'). What you would want to do is Test.prototype.WELCOME = 'Hello' and instances of Test would have that property in their [[proto]] tree and then be able to make use of it.

function Test(input){
  this.output = input;
}
Test.prototype.WELCOME = 'Hello';

var instanceOfTest = new Test('whatever');

// produces this structure:
instanceOfTest => {
  output: 'whatever',
  __proto__: {
    WELCOME: 'Hello'
  }
}

console.log(instanceOfTest.WELCOME); // 'Hello'
Sign up to request clarification or add additional context in comments.

3 Comments

If you use prototype each object will have an instance of WELCOME. If you use it as per my example the property is associated with the 'class' not the instance.
Incorrect. Every instance created using new will reference one singular __proto__. That property will only exist in one place. Test is the constructor, it isn't in the [[proto]] tree. Look at the functions on Object compared to Object.prototype. Then look what hidden methods x has when doing var x = {};.
I understand that each object has a prototype object. All the properties of the prototype will appear as properties of the object. If you look at an instance of my example WELCOME will not be one of the properties. You can only access it via the class name.
3

Assuming you mean:

function Test(input)
{
  this.output = input;
  Test.WELCOME = "Hello";
}

Example 2

function Test(input)
{
  this.output = input ;        
}
Test.WELCOME = "Hello";

Then there will be no difference in terms of the properties of any new Test objects, however in Example 1 you will be assigning a WELCOME property to the Test function on each call to the method, whereas in Example 2 you will only assign this property once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.