- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个关于 Javascript 对象中的公共(public)变量和私有(private)变量的问题。这是我一直在研究的简单代码,用于了解变量范围以及私有(private)和公共(public)属性。
var fred = new Object01("Fred");
var global = "Spoon!";
function Object01(oName) {
var myName = oName;
this.myName = "I'm not telling!";
var sub = new subObject("underWorld");
this.sub = new subObject("Sewer!");
Object01.prototype.revealName = function() {
return "OK, OK, my name is: " + myName + ", oh and we say " + global;
}
Object01.prototype.revealSecretName = function() {
console.log ("Private: ");
sub.revealName();
console.log("Public: ");
this.sub.revealName();
}
}
function subObject(oName) {
var myName = oName;
this.myName = "My Secret SubName!";
subObject.prototype.revealName = function() {
console.info("My Property Name is: " + this.myName);
console.info("OK, my real name is: " + myName + ", yeah and we also say: " + global);
}
}
到目前为止我观察到的有趣的事情是在我的对象中,一个普通的 var 被视为私有(private)的(显然,因为它们在一个函数 block 中),而一个 this
版本是公共(public)的。但我注意到与 this.xxx
同名的变量似乎被认为是不同的变量。因此,在上面的示例中,与我拉取 var myName
的函数相比,我的对象 fred
将为 this.myName
报告一些不同的内容。
但对于我创建的子对象,这种相同的行为并不相同。在 var sub
与 this.sub
的情况下,上面两个都使用 new subObject
调用来创建两个子对象。但似乎 this.sub
和 var sub
都返回了 Sewer!
版本。
Som 我有点困惑,为什么如果我对 this.myName
和 var myName
使用字符串,我会得到两个不同的结果,但我尝试做同样的事情与另一个对象不会产生类似的结果?我猜可能是我用错了它们,或者没有理解 this
和 var
版本之间的区别。
最佳答案
这里最大的问题实际上不是 this
之间的区别基于对象属性和 var
-声明的变量。
你的问题是你试图让原型(prototype)充当一个包装器,它会给你 protected 类属性,这些属性可供子类使用,更不用说你的主类的实例了。
prototype
不能在 "private"
上工作一个类的成员(即在构造函数范围内定义的变量,而不是添加到您要返回的构造对象的属性)。
function Person (personName) {
var scoped_name = personName;
this.name = "Imposter " + scoped_name;
}
Person.prototype.greet = function () { console.log("Hi, I'm " + this.name + "!"); };
var bob = new Person("Bob");
bob.greet(); // "Hi, I'm Imposter Bob!"
点prototype
string 要么提供对对象的可公开访问属性进行操作的方法(例如,如果您想更改 this.name
的值,但您将永远丢失隐藏的 scoped_name
引用)...
...或者如果您希望所有相同类型的对象都可以访问相同的值。
function Student (name, id) {
function showIDCard () { return id; }
function greet () { console.log("I'm " + name + ", and I attend " + this.school); }
this.showID = showIDCard;
this.greet = greet;
}
Student.prototype.school = "The JS Academy of Hard-Knocks";
Student.prototype.comment_on_school = function (feeling) {
console.log("I " + feeling + " " + this.school);
}
var bob = new Student("Bob", 1);
var doug = new Student("Doug", 2);
var mary = new Student("Mary", 1);
mary.school = "The JS School of Closure";
bob.greet(); // I'm Bob and I attend The JS School of Hard-Knocks
mary.greet(); // I'm Mary and I attend the JS School of Closure
mary.comment_on_school("love"); // I love The JS School of Closure
prototype
为 school
定义了默认值, 对于 Student
没有给自己的人。 prototype
还提供了可以在对象之间共享的功能,因为这些功能使用 this
访问对象的实际属性。
函数的任何内部变量ONLY 可以被INSIDE定义的属性或方法访问函数。
所以在这种情况下,prototype
方法可以NEVER 访问 id
, 除了通过 this.showID
,因为 this.showID
是对 showIDCard
的引用功能,这是为每个学生创建的,每个学生都有自己独特的id
,并且他们自己的该函数副本引用了他们自己的该参数的唯一副本。
我对 JS 应用大规模“类”方法的建议是采用一种有利于对象组合的风格。如果你要子类化,让每个子类成为一个模块,有它自己的面向公众的接口(interface),和它自己的私有(private)范围的变量,然后让那个模块成为你试图制作的任何东西的属性,而不是而不是试图让继承链发挥作用。
就是说,如果您期望做一些事情,例如从基类继承,然后将其扩展 8 或 10 代,那么 JS 中的工作太多了。它只会以泪水结束,并提示 JS 不是“OOP”(以您希望的风格)。
关于Javascript OOP 公共(public)和私有(private)变量范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12358440/
据我所知,在 C# 中,所有字段默认都是私有(private)的,如果没有另外标记的话。 class Foo { private string bar; } class Foo { strin
这个问题在这里已经有了答案: Why is it allowed to access Java private fields via reflection? (7 个答案) 关闭 6 年前。 使用反
在 C#(和许多其他语言)中,访问相同类型的其他实例的私有(private)字段是完全合法的。例如: public class Foo { private bool aBool; pu
使用私有(private)方法通过将一些决策点重构为单独的方法来降低 CC 会降低实际方法的 CC 并易于阅读,但不会减少在测试中获得完整分支覆盖的工作量。 这合理吗?你有什么现场经验? 最佳答案 好
在下面的例子中,模块outer有一个私有(private)类型Private和一个私有(private)内部模块inner。 inner 能够访问Private(因为子模块可以访问其父级的私有(pri
class Person def one @var = 99 self.two end private def two p @var end end p=P
我在 Azure 中创建了 VNET。我放入了一个子集 Azure Private Link,它在 VNET 之外和另一台虚拟机中调用 Azure Function。 当我尝试通过专用 IP 调用专用
我在 Azure 中创建了 VNET。我放入了一个子集 Azure Private Link,它在 VNET 之外和另一台虚拟机中调用 Azure Function。 当我尝试通过专用 IP 调用专用
我正在尝试获得良好的 Ruby 编码风格。为防止意外调用具有相同名称的局部变量,我总是在适当的地方使用 self.。但是现在我偶然发现了这个: class MyClass "method" a
今天遇到一个案例类构造函数的奇怪问题。我想将构造函数设为私有(private),看来这不是问题。所以我已经在我的一个项目中尝试过它并且它有效。但在另一个项目中,我可以调用私有(private)构造函数
我想坚持使用记录,并且不想返回对象。所以我想知道是否可以将记录的字段设置为私有(private)?或者创建记录的私有(private)成员。其他具体类型(例如可区分联合)怎么样? 或者,这个要求是否违
我正在通过 Flickr API 进行经过身份验证的调用来访问照片。但我只得到我的公开照片,而没有任何私有(private)照片。 下面给出的是我正在使用的代码, Flickr f; Request
这两个类的行为不同;原因似乎与使用 private[this] 声明而不是 private 有关。有人可以解释一下为什么吗? 私有(private): class Person( private
在 private 中的 1000 秒 private 之后,我想到可能不需要它 public class Outer { private static class Inner { // yo
我有以下代码: class C { private enum E { // ... } } private extension C { func f(e: E)
OOP 语言中是否有object-private 的概念??我的意思是比经典的私有(private)访问限制更多? Private (or class-private) restricts the a
swift 3.0 我知道fileprivate访问级别修饰符将函数/属性的使用限制在声明它的源文件和 private - 仅限于声明的词法范围。但似乎这条规则不适用于扩展。例如。此代码有效: cla
即将推出的 Delphi 版本中启用该功能的功能怎么样? 也许它可能是一个编译器开关,促进所有 ** private **s to ** strict private **小号。 ... 或者它可能是
我可以通过将函数放入类的私有(private)扩展中来创建私有(private)函数,而不是通过不断调用 private func functionName(){} 来创建新的私有(private)函
部署专用端点并需要专用 IP 地址作为输出,但似乎无法正确获取值查询。下面的结果是“模板输出'主机名'无效:语言表达式属性|'privateIPAddress'具有无效的数组索引..(代码:Deplo
我是一名优秀的程序员,十分优秀!