0
function someClass(){
}
someClass.prototype.CONST = 'Some Constant.';

console.log( 'with Obj : '+(new someClass).CONST);
console.log( 'without Obj : '+someClass.CONST);

first one gives right answer, second returns undefined, now, is there any way to access CONST without creating object instance,

I am looking for something similar to accessing static attributes of Java Class

2 Answers 2

4

Yeap, you can access it:

console.log(someClass.prototype.CONST);
Sign up to request clarification or add additional context in comments.

Comments

0

You can access all prototypical members via an object's prototype member, not just variables:

// returns a value
someObject.prototype.someMember

// calls the function someFunct()
someObject.prototype.someFunct()

If you need to call a method from one object, using another object as the 'calling instance' you can use .call or .apply

// calls functMember using instanceObject as the instance
someObject.prototype.functMember.call(instanceObject, arg1, arg2, ... );

// calls functMember using isntanceObject as the instance, using an array as arguments
someObject.prototype.functMember.call(instanceObject, [arg1, arg2, ...] );

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.