
// test.js // > node test.js // define Edge function Edge(x, y) { this._x = x; this._y = y; }; Edge.prototype = { get x(){ return this._x }, set x(val){ console.log('x is readonly') }, // 设置 x 为只读且无法更改 get y(){ return this._y }, set y(val){ console.log('y is readonly') } }; Edge.prototype.showLocation = function() { return 'Location: (' + this._x + ', ' + this._y + ').' }; // use Edge var eg = new Edge(23,23); console.log(eg.x); console.log(eg.y); console.log(eg.showLocation()); // #=> Location: (23, 23). eg.x = 30; // 测试能否被修改,期望不能被改 eg.y = 30; console.log(eg.showLocation()); // #=> Location: (23, 23). 这是我想要的效果 eg.showLocation = function() { return 'I am joking' }; // 测试能否被修改,期望不能被改 console.log(eg.showLocation()); // #=> I am joking 这不是我想要的效果 请问如何让 eg.showLocation = function() { return 'I am joking' }; 这句 失效 即 让 函数不被覆盖 .. ?
1 chairuosen 2016-04-05 15:53:05 +08:00 |
2 charlie21 OP @chairuosen 是我想要的 谢谢 补个英文文档,其中 defineProperty 的 value 不只是 值,还可以是 any valid Javascript value (number, object, function, etc). 这样就可以做到让函数作为 value ,并控制它为只读了 https://developer.mozilla.org/en-US/docs/Web/Javascript/Reference/Global_Objects/Object/defineProperty https://developer.mozilla.org/en-US/docs/Web/Javascript/Reference/Global_Objects/Object/defineProperties https://developer.mozilla.org/en-US/docs/Web/Javascript/Guide/Working_with_Objects#Defining_getters_and_setters |
3 murmur 2016-04-05 16:20:37 +08:00 object.freeze? |
4 w88975 2016-04-05 16:23:19 +08:00 是类似于 const ? |
5 charlie21 OP @w88975 写一个实例方法,并确保它是安全的:让类的实例仅能 “使用” 这个实例方法(而不能去 “覆盖” 这个实例方法,让这个实例方法的方法体不被覆盖。如果想要覆盖,先生成 子类做多态 ) |
7 inmyfree 2016-04-05 17:46:35 +08:00 ! import 哈哈 |
8 charlie21 OP 我不知道我的感觉对不对: js 里的 object , object literal notation 的 method , 类的 prototype 都很容易一不小心就给 override 了 只有 defineProperty 能显式表明 writable false 即 不能被覆盖 |
9 morethansean 2016-04-06 09:16:27 +08:00 @charlie21 如果只是想一个 object 不再被做任何改变,就用 freeze 呗。原型链怎么会被“一不小心就给 override ”,写了那么久的 js 就连属性被 override 带来了 bug 都几乎很少发生。 |
10 ChunlinLi 2016-04-16 22:50:48 +08:00 |
11 qwerasdf 2016-05-05 01:45:53 +08:00 或许你会需要阅读这个 G 强类型 http://blog.csdn.net/rflyee/article/details/44736133 强类型,一旦一个变量被指定了某个数据类型,如果不经过强制转换,那么它就永远是这个数据类型了 强类型定义语言是类型安全的语言。(变量声明类型) 弱类型的变量, 一个变量可以多次赋不同数据类型的值。比如 接连执行 s = 5; s = "five"; 是可以的 强类型定义语言带来的严谨性能够有效的避免许多错误。 强类型 Javascript 的解决方案 http://www.ruanyifeng.com/blog/2015/02/strong-typing-Javascript.html |
12 elrrrrrrr 2016-05-06 13:52:55 +08:00 get 方法? |