JavaScript:继承和常量声明
帮助, 我有这个类,
var jMath = {
pi2: Math.PI,
foo: function() {
return this.pi2;
}
}
我想让 pi2 常量,并且我希望 jMath 从 Math 对象继承。 我怎么做?
Help,
I have this class
var jMath = {
pi2: Math.PI,
foo: function() {
return this.pi2;
}
}
I want to make the pi2 constant and i want jMath to inherit from Math object. How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
哦,有趣的是,从头开始,这是正确的版本:(
与此处的其他答案相匹配)
(我最初尝试使用“JMath.prototype = new Math()”设置原型,这就是我所看到的其他地方,但上面的作品)
编辑
这是作为单例执行此操作的一种方法
Oh amusing, scratch all that, this is the correct version:
(which matches what the other answer is here)
(I originally tried to set the prototype using "JMath.prototype = new Math()" which is how I've seen it other places, but the above works)
Edit
Here's one way to do it as a singleton
考虑使用原型:
这和@altCognito的答案之间的区别在于,这里对象的字段是共享的,并且都指向相同的东西。 如果不使用原型,则可以在构造函数中创建新的未链接的实例。 您可以在每个实例的基础上覆盖原型的值,如果您覆盖它然后决定不喜欢覆盖值并想要恢复原始值,请使用
delete
删除覆盖只是“掩盖”了原型的价值。编辑:如果您想继承Math对象本身的所有方法和字段,但在不影响Math对象的情况下覆盖某些内容,请执行以下操作(根据您的喜好更改名称“Constructor1”):
编辑3:解释Constructor1 函数:这将创建以下原型链:
j 是 JMath 的实例。 JMath 的原型是Constructor1 的一个实例。 Constructor1 的原型是 Math。 JMath.prototype 是被覆盖的东西“生存”的地方。 如果您只实现 JMath 的几个实例,则可以将覆盖的内容作为构造函数 JMath 设置的实例变量,并直接指向 Math,就像 @altCognito 的答案所做的那样。 (j 是 JMath 的实例,JMath 的原型是 Math)
在构造函数中增强对象有 2 个缺点。 (实际上不一定有缺点)一是在构造函数中声明实例字段/方法为每个实例创建单独的值。 如果创建大量 JMath 实例,每个实例的 JMath.foo 函数将是一个单独的对象,占用额外的内存。 如果 JMath.foo 函数来自其原型,则所有实例共享一个对象。
此外,您可以在事后更改 JMath.prototype.foo,实例将相应更新。 如果您在构造函数中将 foo 函数作为每个实例的方法,那么一旦创建了 JMath 对象,它们就是独立的,并且更改 foo 函数的唯一方法是更改每个对象。
编辑2:就只读属性而言,您无法真正从Javascript本身内部实现它们,您需要在表面下进行处理。 但是,您可以声明所谓的“getters”,它实际上充当常量:
警告:定义 getters/setters 的语法 显然 IE 不能很好地实现< /a>.
Consider using
prototype
:The difference between this and @altCognito's answer is that here the fields of the object are shared and all point to the same things. If you don't use prototypes, you create new and unlinked instances in the constructor. You can override the prototype's value on a per-instance basis, and if you override it and then decide you don't like the override value and want to restore the original, use
delete
to remove the override which merely "shadows" the prototype's value.Edit: if you want to inherit all the methods and fields of the Math object itself, but override some things without affecting the Math object, do something like this (change the name "Constructor1" to your liking):
edit 3: explanation for the Constructor1 function: This creates the following prototype chain:
j is an instance of JMath. JMath's prototype is an instance of Constructor1. Constructor1's prototype is Math. JMath.prototype is where the overridden stuff "lives". If you're only implementing a few instances of JMath, you could make the overridden stuff be instance variables that are setup by the constructor JMath, and point directly to Math, like @altCognito's answer does. (j is an instance of JMath and JMath's prototype is Math)
There are 2 downsides of augmenting-an-object-in-the-constructor. (Not actually downsides necessarily) One is that declaring instance fields/methods in the constructor creates separate values for each instance. If you create a lot of instances of JMath, each instance's JMath.foo function will be a separate object taking up additional memory. If the JMath.foo function comes from its prototype, then all the instances share one object.
In addition, you can change JMath.prototype.foo after the fact and the instances will update accordingly. If you make the foo function in the constructor as a per-instance method, then once JMath objects are created, they are independent and the only way to change the foo function is by changing each one.
edit 2: as far as read-only properties go, you can't really implement them from within Javascript itself, you need to muck around under the surface. However you can declare so-called "getters" which effectively act as constants:
Warning: The syntax for defining getters/setters apparently is not something that IE doesn't implement nicely.
用户定义的对象属性不能是常量。 Math(和一些其他对象)是一个特殊的内置对象 - 它具有只读属性和函数。 但它不是一个构造函数 - 它只是一个静态对象(Math.constructor === Object)。
而且由于 JavaScript 具有原型继承,而不是经典继承,因此您不能从 Math 继承。 (在此处阅读更多内容)
但是,您可以做的是定义原型。 当本地找不到属性时,JS 解析器会在当前对象的原型上查找该属性。 altCognito 当前的解决方案很好地证明了这一点。
我很好奇你到底想要实现什么目标。 也许这样的东西就是你想要的?
User-defined object properties can't be constant. Math (and a few other objects) is a special built-in - it has read-only properties and functions. But it's not a constructor - it's just a static object (Math.constructor === Object).
And because JavaScript has prototypal inheritance, and not classical, you can't inherit from Math. (Read more here)
What you can do, however, is define a prototype. When a property isn't found locally, the JS parser looks for that property on the current object's prototype. altCognito's current solutions shows this very well.
I'm curious about exactly what it is you're trying to achieve. Perhaps something like this is what you want?