- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们如何防止下面代码中的危险行为?
var ee = require('events').EventEmitter;
var util = require("util");
util.inherits(strr, ee);
function strr() {
ee.call(this);
this._events = 0; // modify the private member value in parent
}
如果您不知道 this._events 是父 EventEmitter 对象中的私有(private)变量成员,那么内部数据就会被您自己破坏(变异) (继承类)。但我们无法了解父私有(private)成员的所有信息。
上面的代码使用了node.js,问题不太容易理解。我再补充一些
function Parent() {
this.x = 0;
this.y = 0;
this._secre = 1;
}
Parent.prototype.run = function(x, y) {
if (this._secre) {
console.log('run');
}
};
function Child() {
Parent.call(this); // call super constructor.
this._secret = 0; //accidently make the same member name with the parent
}
// subclass extends superclass
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
Child.prototype.show = function() {
this.run();
console.log("Show my secret -> %s", this._secret);
};
var child = new Child();
console.log('Is child an instance of Child? ' + (child instanceof Child)); // true
console.log('Is child an instance of Parent? ' + (child instanceof Parent)); // true
child.show();
它将输出
Is child an instance of Child? true
Is child an instance of Parent? true
run
Show my secret -> 0
但是,如果您不小心将子级的 _secre 成员命名为 _secret,那么您将不会获得“运行输出”
function Parent() {
this.x = 0;
this.y = 0;
this._secre = 1;
}
Parent.prototype.run = function(x, y) {
if (this._secre) {
console.log('run');
}
};
function Child() {
Parent.call(this); // call super constructor.
this._secre = 0; //accidently make the same member name with the parent
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
Child.prototype.show = function() {
this.run();
console.log("Show my secret -> %s", this._secre);
};
var child = new Child();
console.log('Is child an instance of Child? ' + (child instanceof Child)); // true
console.log('Is child an instance of Parent? ' + (child instanceof Parent)); // true
child.show();
最佳答案
不,阻止外部脚本访问“私有(private)”变量的唯一方法是在函数内限制该私有(private)变量的范围:
;(function() {
var private_var;
)());
如果对象的属性要在对象内部使用,但不打算在对象外部访问,则通常使用下划线前缀命名约定。
obj._private
但实际上没有什么可以阻止其他开发人员访问此类属性(如果他们有权访问其父对象)并可能更改其值。
关于javascript继承是危险的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31538260/
编辑:澄清一下,我明白为什么这段代码不起作用,我并不是要修复它,而是想了解如果这段代码可以在没有语义错误的情况下编译会有什么危险。 我发现下面的代码会导致静态语义错误。我知道这是因为 std::lis
我想知道 C# 枚举以及重复值会发生什么。我创建了以下小程序来测试: namespace ConsoleTest { enum TestEnum { FirstElem
正如我在另一个 SO 问题中指出的那样,我遇到了 this article .当我通过 MSVC7.1 编译 boost 1.40 时出现了这个问题,并且弹出了几个 C4251 警告。 现在,在阅读上
我有以下弹出窗口代码(客户端请求)。它使用 eval ,我知道这是危险的。有没有办法重写下面的脚本,使其不使用 (eval)? /* exported popup_default , popup_he
NTFS 文件可以有对象 ID。可以使用 FSCTL_SET_OBJECT_ID 设置这些 ID .然而,msdn article说: Modifying an object identifier c
我一直在学习命令行参数解析。关于这个已经有很长的线索了,我不想在这里挑起一个: Using getopts in bash shell script to get long and short com
这个问题在这里已经有了答案: How to configure ContextMenu buttons for delete and disabled in SwiftUI? (4 个回答) 4 个月
为什么在 linux 的中断处理程序中禁止 printk 或 (I/O)。?在什么情况下中断处理程序中的 I/O 会导致 linux 系统中的死锁? 最佳答案 关于 printk(),它是侵入性的。例
不是 Invoking virtual function and pure-virtual function from a constructor 的重复项: 以前的问题与 C++ 03 相关,而不是
我正在使用 lateinit 属性以避免使用 ?运算符(operator)。我有很多 View 属性是第一次在 getViews() 函数中分配的。如果该功能不存在,我的应用程序将与来自 Kotlin
我最近在使用 fputs 时遇到了问题:当使用 fputs 在文本文件中打印一些字符串时,我碰巧得到了除 A-Z、a-z、0-9 之外的其他字符(不属于字符串的字符) .我绝对确保所有字符串都以空字符
在仅包含字节数组的结构上使用 #pragma pack(1) 是否危险/有风险?例如。这个: #pragma pack(1) struct RpcMessage { uint8_t proto
我是一名优秀的程序员,十分优秀!