1

I´m working with a software where I don't know the global object name(no, it´s not window), but I want to.

console.log(this)

gives me [object object]

for(var property in this) {
    console.log(property + "=" + this.property);
}

gives me the properties of 'this'.

However, I need the name/id(something with what I could access the properties in another contexts/objects) of 'this'. Is it somehow possible to get that?

I already searched for this, but couldn´t find a fitting solution.

Community
  • 1
  • 1
Faizy
  • 299
  • 2
  • 12

1 Answers1

4

You need to get the property value from this context using bracket notation.

for(property in this) {
    console.log(property + "=" + this[property]);
    //                       ----^^^^^^^^^^^^^^----
}

You can get the object properties using Object.keys method which returns the array of property names.


FYI : To access this context within an another context refers it using a variable which has scope within both contexts(commonly you can use a global variable).

// initial variable or neglect to make it as global
var self;

/* cotext 1 start */

// define   
self = this;

for(property in self) {
    console.log(property + "=" + self[property]);
}
/* cotext 1 end  */

/* cotext 2 start */

// use `self` to refer the context1         
for(property in self) {
    console.log(property + "=" + self[property]);
} 

/* cotext 2 end  */
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188