Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8598697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:12:58+00:00 2026-06-12T01:12:58+00:00

let consider i have a object ‘msg’ i re-define it in following cases but

  • 0

let consider i have a object ‘msg’ i re-define it in following cases but i am not understand the behaviour of its property (specially m1). why it is undefined inside the object and it is give string value when i access it through function (last case). can you explain for each case

case 0
    var msg = {
    m1 : "this is string",
    m2 : "ok, " + this.m1

};

console.log(msg.m1); //this is string
console.log(msg.m2); // ok, undefined
/////// why m1 is undefined inside msg
//------------------------------------------
case 1
var msg1 = new Object(msg);

console.log(msg1.m1); //this is string
console.log(msg1.m2); // ok, undefined
//again undefined

//------------------------------------------
case 2
var msg2 = {
    m1 : function () { return "this is string";},
    m2 : "ok, " + this.m1,
    m3 : typeof this.m1

};

console.log(msg2.m1()); //this is string
console.log(msg2.m2); // ok, undefined
console.log(msg2.m3); // undefined
console.log(typeof msg2.m1) // function  'but inside msg2 it is undefined why'

//------------------------------------------
case 3
var msg3 = {
    m1 : (function () { return "this is string";}()),
    m2 : "ok, " + this.m1,
    m3 : typeof this.m1
};
console.log(msg3.m1); //this is string
console.log(msg3.m2); // ok, undefined
console.log(msg3.m3); // undefined
console.log(typeof msg3.m1) // string (atleast i know why this is )  but inside msg2 it is not defined (why )

//------------------------------------------
case 4
var msg4 = {
    m1 : (function () { return "this is string";}()),
    m2 : function () { return "ok, " + this.m1; },
    m3 : typeof this.m1
};
console.log(msg4.m1); //this is string
console.log(msg4.m2()); // ok, this is string 
console.log(msg4.m3); // undefined
console.log(typeof msg4.m1) // string (atleast i know why this is )  but inside msg2 it is not defined and in m2 it evaluated (why so) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:12:59+00:00Added an answer on June 12, 2026 at 1:12 am

    this will never refer to the object that is currently being made via object literal syntax.

    this refers to the environment’s calling context. Objects themselves don’t have a calling context, but they can be used as a calling context.

    Notice your msg4.m2() works. That’s because this is the calling context of the function, and a reference to the object on which the method was invoked, which is the msg4 object.

    How did msg4 become the calling context of the m2 function? It’s because when you do this:

    msg4.m2();
    

    you are invoking m2 on the msg4 object. This automatically sets up the calling context so that this points to the msg4 object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's consider following, simplified example: We have 2 tabs withing <rich:tabPanel switchType=ajax> , each
I have an ObservableCollection<object> Let's consider we have 2 items in it : int
Let's consider I have the following code /*...*/ var _fun = fun; fun =
Let's consider we have QWidget that contains QTableWidget (only). So we want to resize
Let's consider the below example. There, I have: target MAIN calls target t and
Consider that I have 1 resource and 2 urls (let's say new one and
Let's consider the following scenario: a function which can generate code colors from white
Let's consider the following enum in C# public enum ScrollMode : byte { None
Possible Duplicate: JavaScript property access: dot notation vs. brackets? Let's consider I have an
Please let me know which one is good Consider I have a class like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.