0

I am not sure about differences between object literal and constructor function.

function user() {
    this.name="john"
    this.getName=function (){
        return this.name;
    };
}

var johnUser=new user();
console.log(johnUser.getName()); // Will console john

What if I want to call getName without creating any objects something like in Java static method/function?

If I can add

user.sayName=function() {
    console.log("Hey there");
}

console.log(user.sayName()); //will console Hey there.

How I can access constructor function properties?

4
  • The second block with user.sayName = function... works. Does that not do what you want? Commented Mar 12, 2017 at 10:00
  • It appears that you are coming to JavaScript hoping that it's Java for the browser. It isn't. You should spend some time trying to grok the prototype inheritance model, which is pretty significantly different from classical inheritance. He's pretty judgmental in many ways and don't take his word as gospel, but I think you may pick up a lot from these videos: youtube.com/watch?v=riDVvXZ_Kb4 youtube.com/watch?v=wfMtDGfHWpA Commented Mar 12, 2017 at 10:11
  • Thank you all and what if i want to call sayName function through johnUser object...is it possible? Commented Mar 12, 2017 at 10:52
  • @john33, this may help you: stackoverflow.com/q/35156270/1911755 Commented Mar 13, 2017 at 14:00

3 Answers 3

2

The answer is You can't. You should have read about how context of function works in JS.

When You are using new operator, empty object is being passed as context, that's why You can assign properties of this (which is reference to function context)

When You are calling a function without new operator, context of Your function is eiter global object (window) or undefined. You can print this in function to see what context You have ATM.

What You (probably) want is to create an prototype

Sign up to request clarification or add additional context in comments.

1 Comment

Judging by the statement "without creating any objects something like in java static method" I think he's actually just looking for a simple object with functions like a helper. It's often hard for Java devs to come to term with JavaScript's lack of statics.
0

Object literal

var user = {
    name: "John",
    getName: function(){
        return this.name;
   }
};

Alternatively, this approach won't define an object.

function sayName()
{
    return "just a string";
}

2 Comments

I assume people might be downvoting you because you're creating an object when the question specifically asks how to do it without creating an object, but this is a completely appropriate way of doing what OP is asking for. getName is a terrible example however since what OP says he's looking for is an equivalent of Java static methods which would not access instance parameters (and name sounds like an instance parameter even though it's actually mimicking a class level one here).
He might be looking for function sayName() { return "just a string literal"; }
0
function user() {
    this.name = 'john';
}

user.prototype.getName = function(){ return this.name; }

var johnUser = new user();

console.log( johnUser.getName() )       // john
console.log( user.prototype.getName()); // undefined

user.prototype.getName = function(){ return 'just a string' }

console.log( johnUser.getName() )      // just a string
console.log( user.prototype.getName());// just a string

user.prototype.getName = function(){ return this.name || 'just a string'; }

console.log( johnUser.getName() )      // john
console.log( user.prototype.getName());// just a string

2 Comments

Thank you again but i don't want to call prototype property function instead i want to call this function user.sayName=function(){ console.log("Hey there"); } literal through johnUser.
pseudo-code, similarity: function user(){} === user.function(){}; user.sayName=function(){}; /* user { function : (){}, sayName : { function : (){} }} */ and when calling user() ( or new user() ) executed some like this user.function(), and sayName not inside function

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.