Javascript 中的静态变量继承 (ES6)
Static variable inheritance in Javascript (ES6)
我试图弄清楚Javascript如何完全支持OOP。幸运的是,我可以通过 Babel 找到一些线索,并知道它如何向下兼容 ES5。
但是我发现静态变量行为在继承中很奇怪。
例如,我想记住超类中的全局属性。但似乎从子类访问的静态变量实际上并不是指超类。这在经典 OOP 中合理吗?
class Animal {
constructor(){
this.constructor.count += 1;
console.log('An animal was born');
}
static count = 0;
static sum = function(){
console.log('There are', this.count, 'animals');
}
}
class Cat extends Animal{
constructor(){
super(); // throws exception when not called
console.log(' -- the animal is a cat');
}
}
var cat1 = new Cat();
var cat2 = new Cat();
Cat.sum(); // should be 2
Animal.sum(); // should be 2, but result is 0
↑ 在巴别塔实验模式中
以上是经验语法。然后我看到一篇文章说 ES6 还不支持静态属性。所以我按照他的例子重写为静态方法(getter/setter),风格,但仍然不知道.....
class Animal {
constructor(){
this.constructor.countOne();
console.log('An animal was born');
}
static countOne(){
this.count = (this.count||0)+1;
}
static sum(){
console.log('There are', this.count, 'animals');
}
}
Animal.count = 0; // Remove this, Animal.sum() will be undefined
class Cat extends Animal{
constructor(){
super();
console.log(' -- the animal is a cat');
}
}
var cat1 = new Cat();
var cat2 = new Cat();
Cat.sum(); // should be 2
Animal.sum(); // should be 2, but result is 0
↑ ES6 小提琴
"这个"指的是子类,不是超类,结果是一样的...
此外,我在 PHP 中尝试相同的代码,然后我得到了预期的结果:
class Animal{
static $count = 0;
static function sum(){
echo "There are " . self::$count . " animals <br>";
}
public function __construct(){
self::$count++;
echo "An animal was born <br>";
}
}
class Cat extends Animal{
public function __construct(){
parent::__construct();
echo " - the animal is a cat <br>";
}
}
$cat = new Cat();
$cat = new Cat();
$cat = new Cat();
Cat::sum(); // is 3
Animal::sum(); // is 3
到目前为止,我们应该说Javascript不支持静态变量继承吗?即使在ECMA6中?
有什么优雅的解决方案吗?
您可以访问静态成员,例如:
Animal.count;
Animal.countOne();
Animal.sum();
在创建新 cat 的第二个示例中,this
引用新的 cat 对象,this.constructor
引用 Cat 函数(即使它是从超构造函数调用的)。
还有另一种提供静态属性的方法,那就是通过闭包。通过将类定义包装在函数中,您可以仅将变量的作用域限定为类,从而有效地创建私有静态变量。
例如
"use strict";
var log =function(d){console.log(d)}; // lazy zoo keeper
// need to define the intermediate container
// Ill call it zoo.
var zoo = (function() {
// now create the private static property
var count=0; // function scoped
class Animal {
constructor(){
count += 1; // count instances
log('An animal was born');
}
static sum(){ // create the static method of interagation
log('There are'+count+'animals');
}
}
class Cat extends Animal{
whatAreYou(){log("I am a cat ")};
}
// now return the classes you want to expose
return {
Animal:Animal,
Cat:Cat,
};
})(); // call the function to create a Zoo
// now you can make the the Animal and Cat public or you could
// keep zoo and pass it to another scope and have them private
// where you want.
var Animal = zoo.Animal;
var Cat = zoo.Cat;
// Use static function befor there are any instances of Animal or Cat
Animal.sum(); // displays 0
var a = new Animal(); // or new zoo.Animal();
var c = new Cat();
// access static function sum to display content of private and static (closure) property count;
Cat.sum(); // 2
Animal.sum(); // 2
在Animal
构造函数中,将this.constructor.count
更改为Animal.count
。
-
this.constructor.count
是指Cat
类计数器 -
Animal.count
是指Animal
计数器
即:
class Animal {
constructor(){
Animal.count += 1;
console.log('An animal was born');
}
重点是每个类都有自己的静态变量,即它不共享。JavaScript不支持传统意义上的类,他们说它的语法糖超过它使用的对象原型模型。您可能会发现此链接很有用: https://github.com/getify/You-Dont-Know-JS/tree/2nd-ed/objects-classes
- ES6构造函数返回基类的实例
- 如何在下面的ES6循环中获得前面的文本
- es6 相当于下划线查找位置
- 从控制器继承了隔离的作用域以生成可重用的指令
- 如何在Javascript/ES6中的Aurelia浏览器应用程序中使用AWS S3
- 两个指令创建新的继承的和隔离的作用域-元素得到哪个
- ES6生成器:.next()的输入值
- 简单的ES6承诺问题-交换解决和拒绝参数
- 映射数组ES6时考虑空值
- AngularJS 1.X ES6继承问题
- 无法在 es6 继承的类中设置属性
- ES6 类继承的性能
- ES6 多重继承
- Javascript 中的静态变量继承 (ES6)
- 从非类继承ES6/TS类
- 当涉及继承时,存储ES6 Javascript类的getter/setter
- 在ES6中将一个继承的类复制到另一个
- 在es6中是否有一种方法可以从错误中继承而不将构造函数放在堆栈跟踪中?
- 在这个类继承中使用了什么ES6/React魔术
- React ES6组件继承:工作,但不推荐