我试图找出将静态(或类)属性设置为ES6类的替代方法,然后在创建类的新实例后更改它.
例如,假设我有一个名为的类Geo,我需要一个名为的静态属性all,它将为我提供Geo该类所有实例的数组.
这个版本有效:
class Geo {
constructor(name){
this.name = name;
Geo.all.push(this);
}
}
Geo.all = [];
ruby = new Geo("Ruby");
rocks = new Geo("Rocks");
console.log(Geo.all.length); // => 2
Run Code Online (Sandbox Code Playgroud)
我宁愿不设置类定义的属性OUTSIDE.我尝试了一些东西,但似乎无法在类中创建一个静态属性,我可以从构造函数更新.
我还要提一下,我需要能够在浏览器(Chrome)中执行此操作而不使用Babel或类似功能.
以下是我尝试过的一些事情的例子:
class Geo {
constructor(name){
this.name = name;
Geo.all.push(this);
}
static get all() {
return [];
}
}
ruby = new Geo("Ruby");
rocks = new Geo("Rocks");
console.log(Geo.all.length); // => 0
Run Code Online (Sandbox Code Playgroud)
而另一个
class Geo {
constructor(name){
this.name = name;
Geo.all.push(this);
}
static all = []; …Run Code Online (Sandbox Code Playgroud) 我正在构建一个Express应用程序并遇到一些路由问题.我的'/'路线工作正常,但其他路线不是.我已经调查了人们发布的其他问题,这些问题都没有解决我的问题.
我有一个routes/index.js文件:
module.exports = function(app){
app.use('/', require('./routes/home'));
app.use('/about', require('./routes/about'));
}
Run Code Online (Sandbox Code Playgroud)
我的路线/ home.js: - 工作!
const express = require('express');
const router = express.Router();
router.get('/', function(req, res) {
res.render('app/home');
});
module.exports = router;
Run Code Online (Sandbox Code Playgroud)
我的路线/ about.js: - 不工作!
const express = require('express');
const router = express.Router();
router.get('/about', function(req, res) {
res.render('app/about');
});
module.exports = router;
Run Code Online (Sandbox Code Playgroud)
当我转到'/ about'时,我在浏览器中看到了这个错误 - '无法获取/关于'
home.html和about.html文件都位于相同的views目录中.
这里的任何帮助将非常感谢!
javascript ×2
class ×1
ecmascript-6 ×1
express ×1
node.js ×1
object ×1
oop ×1
routing ×1
server ×1