Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS是否可以访问函数变量?_Javascript_Function - Fatal编程技术网

Javascript JS是否可以访问函数变量?

Javascript JS是否可以访问函数变量?,javascript,function,Javascript,Function,我的理解是,JavaScript中的“函数”是函数,但也可以是构造函数,也可以是对象本身。如果我设计的函数既像函数(带有返回),又像构造函数,那么我能访问返回值和函数范围之外的属性吗?像这样 function Tree() { this.name = "Boris"; var age = 18 var cookies = "cookies" // defined with var instead of this.cookies return age; }

我的理解是,JavaScript中的“函数”是函数,但也可以是构造函数,也可以是对象本身。如果我设计的函数既像函数(带有返回),又像构造函数,那么我能访问返回值和函数范围之外的属性吗?像这样

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
那么,我可以用同一个对象访问名称、年龄和cookies吗?我越想越想,出于安全原因,我想做的可能不可能,但我还是想知道

使用
obj.constructor()。注意:前缀为
new
将返回另一个对象,而不是返回值

 var r2 = new Tree();   // r2 == { name: "Boris" }
 r2.constructor()       // 18
 new r2.constructor()   // { name: "Boris" }, not what I want
我找不到访问用var定义的变量的方法。帮助

那么,我可以用同一个对象访问名称、年龄和cookies吗

No,这种“不可访问性”使得javascript中的私有变量和封装成为可能


允许访问所有内容的示例:
不,不能从函数中获取变量。原因是-,看一看

但是,如果您仍然希望获取变量,我建议您以以下方式编写代码:

function Tree() {
    this.name = "Boris";
    this.age = 18;
    return this;
}
因此,如果希望访问变量,则需要使用

您可以在prototype中定义属性。例如:

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
function Cat(name) {
  this.name = name;
  return "my name is " + this.name;
}

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

但是,如果此变量在函数范围内关闭,则无法访问用
var
定义的变量。

使用
new
关键字调用函数时,函数被视为构造函数。例如:

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
function Cat(name) {
  this.name = name;
  return "my name is " + this.name;
}

var a = new Cat("barry");
var b = Cat("larry");
这里,
a
被分配给使用
Cat
函数作为构造函数的结果,而
b
是调用
Cat
作为普通函数的结果
typeofb
将是
“string”
,因为结果只是函数的返回值

类型的
将是
“object”
,因为new关键字改变了表达式行为-它创建了一个新对象,并以这样的方式调用构造函数(称为绑定)
变量是对该对象的引用。该表达式的结果是
this
对象,而不是您提到的返回值


您所描述的似乎是缺少私有变量,即在对象函数中可以访问的变量(例如
a.addOneToAge()
),但不能通过
a.age
或类似的直接引用。虽然在JavaScript中使用闭包通常是可能的,但这在对象上是不可能的(我知道)。

虽然它解决了这个问题,但问题是函数是在每个树实例上定义的,而不是在Tree.prototype上定义一次(我确信您知道,但我认为我应该明确)@jackweirdy如果这些方法是私有的,那么在原型上定义这些方法会是什么样子呢?函数在每个树实例上都定义了,因为示例使用闭包进行封装。如果你不想麻烦,最好做
this.age=18
this.cookies=“cookies”
@Volune你说的“排序私有”?“排序私有变量和封装”,因为没有其他语言可以提供的私有变量和封装。为什么要使用Tree.prototype设置年龄?@jackweirdy您可以通过多种方式设置默认值。为原型设置值可以是帮助为对象设置默认值的技术。如果
tree
实例将具有
this.age=30
,则
tree.age==30
。但如果
this.age
未定义,则
tree.age==18
。这只是众多变体中的一个。当然,您也可以通过构造函数中的
this.age=age | | 18
来实现。您可以决定使用什么:)您对绑定的解释很有用。你怎么说私有变量可以通过闭包而不是对象实现。@Volune的实现不会为对象上的var创建getter方法吗?这正是Volune所做的-因为函数在构造函数中(这里用封闭的词可能更好),函数可以访问变量。问题是,该函数是在每个实例上构造和复制的。这种性能影响可能可以忽略不计,JavaScripty要做的事情是在原型上定义一次函数——但这样就不能将变量密封起来。