1

i have created a constructor function in javascript

function hello () {
    name = 'shahin';
    age= 22;
    mesg = function () {
        return 'My name is ' + this.name + 'and i am '+ this.age + 'old';
    }
}

console.log(hello.mesg());

and instead creating of new constructor from it i just wanted to whether it is working as a normal function or not. hence try with the console and see that error : "TypeError: hello.mesg is not a function.

`

function hello () {
    this.name = 'shahin';
    this.age= 22;
    this.mesg = function () {
        return 'My name is ' + this.name + ' and i am '+ this.age + ' years old';
    }
}

console.log(hello.mesg())

I even try with this and got the same error

2
  • 4
    Your first one just creates a bunch of global variables. Commented Nov 27, 2017 at 13:43
  • If you call a constructor function without the new keyword, this will be bound to window. Commented Nov 27, 2017 at 13:55

2 Answers 2

1

To understand your question properly, you need to know the return value of a function.

(function(){
    var a = 1;
})();

This returns undefined, since there's no any return value designated.

(function(){
    var b = 2;    
    return b;
})();

This obviously returns b.

Can you distinguish the difference? So, the case #1 and case #2, hello function doesn't point anything as a return value, that's why it returns undefined and you couldn't be able to access mesg method.

To work this out properly, there're bunch of possible ways, I'll give you one of examples.

function hello() {
    var name = 'shahin';
    var age= 22;
    var mesg = function () {
        return 'My name is ' + name + 'and i am '+ age + ' old';
    };

    return {
        getName: mesg
    };
}

var func = hello();
func.getName(); // print 'My name is ... '
Sign up to request clarification or add additional context in comments.

2 Comments

getName will print "My name is undefinedand i am undefinedold" - Remove all this references and use lets/vars
you still need to remove this, otherwise you're adding the mesg function to window
0

In the second case, mesg is the instance's variable of hello, you need to invoke it by instantiating with new operator.

console.log((new hello()).mesg())

In the first case, you can't invoke mesg as a property. You invoke mesg only if you return the mesg function

function hello () {
    name = 'shahin';
    age= 22;
    return mesg = function () {
        return 'My name is ' + this.name + 'and i am '+ this.age + 'old';
    }
}
console.log(hello()());

6 Comments

Not in the first case it's not
It's not a property of the prototype in either version.
The way OP is using hello, just calling mesg directly will work (in both cases).
where's the second function?
thank you @gurvinder372 what if there is two function inside this function hello () { name = 'shahin'; age= 22; return mesg = function () { return 'My name is ' + this.name + 'and i am '+ this.age + 'old'; } return mesg1 = function () { return 'My name is ' + this.name + 'and i am '+ this.age + 'old'; } } how can i evoke the 2nd function there? i mean mesg1()
|

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.