0

Example, lets say I have the following Constructor function defined in window global of a browser.

function AccessProperty() {
  this.myName = "Chris";
}

Now I execute this function like this:

new AccessProperty();

Now based on how I executed the function above with the new keyword, how can I access this.myName property without adding something like this:

var acccess = new AccessProperty();
7
  • 2
    (new AccessProperty()).myName like this? Commented Sep 24, 2018 at 23:03
  • access.myName Commented Sep 24, 2018 at 23:03
  • @MinusFour, is there any other way besides that? Commented Sep 24, 2018 at 23:06
  • 1
    Sounds like an xy problem to me. If you are not "storing" the value returned by new AccessProperty() somewhere, then you cannot access that value. Can you provide more information? Commented Sep 24, 2018 at 23:13
  • On a high level you seem to be asking whether AccessProperty can perform side effects and store the value in a different place. You could change the implementation of AccessProperty to var obj = {}; function AccessProperty() { obj.myName = "Chris"; }; new AccessProperty(); console.log(obj.myName); but I wouldn't advice doing that. It's hard to help without knowing the actually problem you are trying to solve. Commented Sep 24, 2018 at 23:30

3 Answers 3

3

You can access it directly like this:

acccess.myName

or use the square brackets:

acccess['myName']

Without using the new operator

If you call that function without the new operator, the property will be set in the window object of the page. This is not very recommended, but you could do:

AccessProperty();
console.log(window.myName);

Another possibility is to return a new object like this:

function AccessProperty() {
    return { myName: "Chris" };
}
var o = AccessProperty(); // not using 'new'
console.log(o.myName);
Sign up to request clarification or add additional context in comments.

4 Comments

From the question: "without adding something like this: var acccess = new AccessProperty();"
FWIW, the first one only works in non-strict mode, in strict mode this will be undefined. I also don't think the OP has problems with using new but with assigning the return value to a variable. But tbh, the question doesn't make much sense to me ¯\_(ツ)_/¯.
@FelixKling I agree, the question is not very clear. I've explained some possibilities in order to provide some guidance. Thank you for the extra notes.
correct @FelixKling, without adding a reference to the new object and was thinking it defaulted to the window object but that is not correct either.
1

It's really not clear what your ultimate goal is. this is a contextual reference that changes depending on the context of a function. When you use new, this refers to an object the js system creates for you and returns to the caller. Without new this will be the global window if you just call the function.

You can explicitly tell the function what this is with a function like call():

function AccessProperty() {
    this.myName = "Chris";
  }

let someObject = {}
AccessProperty.call(someObject)   // this will be someObject
console.log(someObject.myName)    // to which the function adds myName.

But that's seems like the hard way to do something, when you could just use new and get an object with a myName property.

As a demonstration of the dynamic nature of this consider the code:

function AccessProperty() {
  this.myName = "Chris";
}

let o = {
  set: AccessProperty,
  talk() {console.log(this.myName)}
}
// because of the way it's called `this` in
// the function AccessProperty will be
// the object o 
o.set()   
o.talk()
console.log(o) // o now has a myName property

This behavior is very useful in some situations, if you don't want that behavior you are required to use this you can just define variables.

2 Comments

my ultimate goal is that the "this" keyword in this case is not bound to anything...
I don't really know what that means @Nora. this is a reference to some object, if it's not a reference to some object, then how can it have properties like myName? Are you sure you don't just want a variable: let myName = "Chris"?
0

If you are not in strict mode you can do something like this

function AccessProperty(){
 this.name="ram"
}

function parent(){
  AccessProperty.bind(this)
  AccessProperty()
  console.log(this.name)
}


parent()

Here we have binded the scope of AccessProperty function to the scope of parent function. With this done the name will be in the scope of parent function

8 Comments

this inside parent will either be undefined in strict mode or window in non-strict mode. This only works accidentally because this is window. AccessProperty is not bound because you are not doing anything with the return value of bind (bind doesn't magically change AccessProperty itself). If you do parent.call({}) instead (forcing this to be an object other than window) you will see that it does not work.
@FelixKling yes you are correct so what other options do we have
The question itself doesn't make much sense to me in its current state, I hope the OP will clarify.
The "this" keyword is not bound to anything. I was thinking it was bound to the window object by default or the object....
@Nora this keyword is bound to window object by default in this case for browsers
|

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.