如何将私有变量添加到这个 Javascript 对象文字片段中?
在 MDC 找到了这个,但是如果我想将私有变量添加到
var dataset = {
tables:{
customers:{
cols:[ /*here*/ ],
rows:[ /*here*/ ]
},
orders:{
cols:[ /*here*/ ],
rows:[ /*here*/ ]
}
},
relations:{
0:{
parent:'customers',
child:'orders',
keyparent:'custid',
keychild:'orderid',
onetomany:true
}
}
}
我理解 Javascript 中的 OOP 的方式,我可以访问 dataset.tables.customers.cols[0] 如果这样的项目存在。
但如果我想将私有变量放入客户中,那会是什么样子?
添加 var index = 0;
会导致运行时错误。
Found this at MDC but how if I'd wanted to add a private variable to the
var dataset = {
tables:{
customers:{
cols:[ /*here*/ ],
rows:[ /*here*/ ]
},
orders:{
cols:[ /*here*/ ],
rows:[ /*here*/ ]
}
},
relations:{
0:{
parent:'customers',
child:'orders',
keyparent:'custid',
keychild:'orderid',
onetomany:true
}
}
}
The way I understand OOP in Javascript, I'd have access to dataset.tables.customers.cols[0] if such an item exists.
But if I wanted to place a private variable into customers, what would that look like?
Adding var index = 0;
results in a runtime error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果不涉及函数,就不能拥有“私有”变量。函数是在 javascript 中引入新作用域的唯一方法。
但不用担心,您可以在正确的位置添加函数,以便为您的对象获得此类功能,
但这并没有给我们带来太多好处。这仍然主要只是一堆文字对象。这些类型的“私有”变量的含义为零,因为没有方法 - 没有任何方法可以实际读取或以其他方式使用我们通过添加函数(闭包)创建的范围内的变量。
但如果我们有一种方法,它实际上可能会开始变得有用。
You can't have "private" variables without a function involved. Functions are the only way to introduce a new scope in javascript.
But never fear, you can add functions in the right place to gain this sort of functionality with your object
But this doesn't gain us much. This is still mostly just a bunch of literal objects. These types of "Private" variables have zero meaning since there are no methods - nothing that would actually read or otherwise use the variables in the scope we created by adding a function (a closure).
But if we had a method, that might actually start to become useful.
JavaScript 缺乏在更严格的语言中获得的访问控制。您可以使用 closures 模拟对象数据的私有访问,但您的示例是对象文字- 一个简单的数据结构 - 而不是一个构造的对象。
这取决于您想要对对象执行什么操作 - “私有”成员的常规技术意味着它们只能通过成员函数访问,并且要求您使用构造函数来创建对象。文字语法用于数据结构或具有公共数据和函数的轻量级对象。
使用私有闭包模式的问题在于,文字中的字段位于公共范围内,但闭包提供的隐私是因为变量是在函数中定义的,因此其作用域是本地的。您可以创建一个克隆文字并添加私有字段的函数,或者添加一个包含私有数据的公共字段。您还可以添加闭包作为成员,因此创建方法私有而不是对象私有的私有字段。
因此dataset.secretCounter()有一个变量c,它仅对该函数私有。
JavaScript lacks the sort of access controls you get in more rigid languages. You can simulate private access for objects' data using closures, but your example is an object literal - a simple data structure - rather than a constructed object.
It rather depends what you want to do with the object - the normal technique for 'private' members means they are accessible only by member functions, and requires that you use a constructor to create the object. The literal syntax is used for data structures or light weight objects with public data and functions.
The problem with using the private closure pattern is that the fields within a literal are in public scope, but the privacy given by the closure is because the variable is defined in a function, so is scoped locally. You could either create a function which clones the literal and adds private fields, or add a public field which has private data. You also can add closures as members, so create private fields which are method private rather than object private.
So
dataset.secretCounter()
has a varablec
which is private to that function only.javascript 中的私有变量是使用闭包内的 var 关键字来完成的。
只有特权方法和属性才能访问它。
方法如下:
Private variables in javascript are done using the var keyword inside a closure.
Only priviliged methods and attributes can access it.
Here's the way to do it: