0
var x = new y ('hello');

function y (mes){
    this.mes = mes;

    //x.mes = mes;
    //Why using object name will not work, with this it will work. 
    //please read the description what i am trying to ask.
}

Explanation of the problem

I am learning Javascript, From MDN,

I am on the topic working with Object. link is https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

I am on the topic Using Object Constructor Function

In here they are basically saying if you want to create similar object use this object constructor function.

First I notice they are using keyword this in the function code. I was kinda confuse, than I google it why they are using this keyword instead of simply using variable.

Than it kinda make sense, as what I understood by my google search is this, that this keyword is refer to the object means object which I am initiating with the keyword new,

so I thought I should be able to use object name directly instead of using the this.

I will just initiate the object before the function, and than I should be able to use the object name, and it did not work.

(I am aware if I do this, it actually defeat the purpose of using Object Constructor Function, but that not the point is, the point is do I understand what this means in this context or why it did not work.)

4
  • Also, you forgot to define "it doesn't work". What does it mean exactly? Commented Mar 27, 2018 at 5:05
  • @SergioTulentsev i know you have two many point but please read the question, second javasscript put the function at the top with the declration, and third i know i said it in the bold read the last lines which i typed in the bold Commented Mar 27, 2018 at 5:15
  • There's also this temporal disconnect. x is not fully defined/initialized until after y returns, but you're trying to access it while y still runs. Commented Mar 27, 2018 at 5:18
  • @SergioTulentsev i got your last point, but what i am confuse about it should behave same with this key word, but it does not, so i am thinking i got some thing wrong about the this keyword, thanks for the last comment. It give me some explanation Commented Mar 27, 2018 at 5:23

1 Answer 1

1

In short

Using var you are declaring local variable in constructor function, It will not accessible outside the constructor function

this is not a variable It is a property of the object, this will survive as long as the object survive

In below example we can not access a in call function but by using this we can access b

Es6 Class

let suppose you declare Test class

class Test{
  constructor(){
    var a = "hello";
    this.b = "world";
   }
  call(){
     console.log(t.b);
     console.log(t.a);
  }
}


var t = new Test();

console.log(t.b); //will print "world"

console.log(t.a); //will print "undefined"

same concept with normal function

function Test() {
    var a = "hello";
    this.b = "world";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nishant i think i got the answer from you, but i am not able to understand, can you try to explain the first two lines which you wrote in your answer, thanks. In short Please can you explain the first lines, becuase if i do understand those lines i will be able to get it, thanks

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.