简体   繁体   中英

JS Is it possible to access function variable?

My understanding is that with JavaScript a "function", is a function, but can also be a constructor, and an object itself. If I design a function like a function (with a return), but also like a constructor, can I ever have access to both the return value, and that property outside of the function scope? Like so

function Tree() {
    this.name = "Boris";
    var age = 18
    var cookies = "cookies"    // defined with var instead of this.cookies

    return age;
}

var r1 = Tree();       // r1 == 18
r1.name                // undefined
var r2 = new Tree();   // r2 == { name: "Boris" }, but no age or cookies

So can I ever access name, age, and cookies with the same object? The more and more I think this over, what I want to do probably isn't possible because of security reasons, but I still would like to know.

Using obj.constructor() you once again have access to the function you used to create the obj, and thus the return value. Note: prefixing with new will return another object, not the return value.

 var r2 = new Tree();   // r2 == { name: "Boris" }
 r2.constructor()       // 18
 new r2.constructor()   // { name: "Boris" }, not what I want

I couldn't find a way to access variables defined with var. Help?

No, you can't get variable from function. The reason for it - function scopes , take a look.

But if you still want to get variables, I propose make your code in this way:

function Tree() {
    this.name = "Boris";
    this.age = 18;
    return this;
}

So you need use this if you want get access to variables.

OR

You can define properties in prototype. For example:

function Tree() {
    this.name = 'Boris';
    return this;
}

Tree.prototype = Object.create({
    age: 18
});

var tree = new Tree();
tree.name == 'Boris'; // true
tree.age == 18; // true

But you can't get access to variables defined with var if this variable closed in function scope.

So can I ever access name, age, and cookies with the same object?

No , and this "non-accessibility" contributes to make sort of private variables and encapsulation possible in javascript.


Example which allow access to everything:

function Tree() {
    if (!this) return new Tree();
    this.name = "Boris";
    var age = 18
    var cookies = "cookies"    // defined with var instead of this.cookies
    this.getAge = function() { return age; };
    this.getCookies = function() { return cookies; };
}

A function is considered to be a constructor when called with the new keyword. For example:

function Cat(name) {
  this.name = name;
  return "my name is " + this.name;
}

var a = new Cat("barry");
var b = Cat("larry");

Here, a is assigned to the result of using the Cat function as a constructor, whereas b is the result of calling Cat as a plain function. typeof b would be "string" as the result is simply the return value of the function.

typeof a would be "object" , as the new keyword changes the expression behaviour - it makes a new object, and calls the constructor function in such a way (known as binding ) that the this variable is a reference to that object. the result of that expression is the this object, not the return value, as you noted.

What you seem to be describing is wanting private variables - that is, variables which are accessible within object functions (eg a.addOneToAge() ), but not from direct reference via a.age or something similar. While generally possible in JavaScript using closures, this isn't possible (that I'm aware) on objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM