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?

dakab
  • 5,379
  • 9
  • 43
  • 67
john33
  • 61
  • 2
  • 8
  • The second block with `user.sayName = function...` works. Does that not do what you want? – nnnnnn Mar 12 '17 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: https://www.youtube.com/watch?v=riDVvXZ_Kb4 https://www.youtube.com/watch?v=wfMtDGfHWpA – Vala Mar 12 '17 at 10:11
  • Thank you all and what if i want to call sayName function through johnUser object...is it possible? – john33 Mar 12 '17 at 10:52
  • @john33, this may help you: http://stackoverflow.com/q/35156270/1911755 – Raphael Rafatpanah Mar 13 '17 at 14:00

3 Answers3

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

grzesiekgs
  • 463
  • 2
  • 11
  • 1
    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. – Vala Mar 12 '17 at 10:14
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";
}
Vic
  • 703
  • 6
  • 9
  • 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). – Vala Mar 12 '17 at 10:07
  • He might be looking for `function sayName() { return "just a string literal"; } ` – Vic Mar 12 '17 at 10:11
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
MypKOT-1992
  • 191
  • 3
  • 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. – john33 Mar 12 '17 at 11:05
  • 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` – MypKOT-1992 Mar 12 '17 at 14:55