Javascript ES 6-使用类

Javascript ES 6-使用类,javascript,node.js,express,Javascript,Node.js,Express,我正在学习ES6类的语法。我有C#的背景,如果我的术语不正确,我道歉。或者,如果我正在做一些看起来很奇怪的事情 我正在构建一个web应用程序作为学习练习。它构建在Node和Express上。我有一些路线定义如下: 'use strict'; module.exports = function() { const app = this; app.use('/blog', function(req, res) { console.log('loading blog

我正在学习ES6类的语法。我有C#的背景,如果我的术语不正确,我道歉。或者,如果我正在做一些看起来很奇怪的事情

我正在构建一个web应用程序作为学习练习。它构建在Node和Express上。我有一些路线定义如下:

'use strict';

module.exports = function() {
    const app = this;

    app.use('/blog', function(req, res) {
        console.log('loading blog postings');        
        res.render('blog', {}); 
    });

    app.use('/', function(req, res) {
        console.log('looking up: ' + req.path);
        res.render('home', {});
    });
};
'use strict';

module.exports = function() {
    const app = this;
    const ViewModels = require('../viewModels/index');

    app.use('/blog', function(req, res) {
        console.log('loading blog postings');
        let viewModel = new ViewModels.BlogViewModel();
        res.render('blog', viewModel); 
    });

    app.use('/', function(req, res) {
        console.log('looking up: ' + req.path);
        let viewModel = new ViewModels.HomeViewModel();
        res.render('home', viewModel);
    });
};
我试图在这些视图后面放置一些视图模型。因此,我有一个名为
viewModels
的目录。该目录包含以下文件:

index.js
blog.js
home.js
当前的文件可能不准确,如下所示:

'use strict';

module.exports = function() {
    const app = this;

    app.use('/blog', function(req, res) {
        console.log('loading blog postings');        
        res.render('blog', {}); 
    });

    app.use('/', function(req, res) {
        console.log('looking up: ' + req.path);
        res.render('home', {});
    });
};
'use strict';

module.exports = function() {
    const app = this;
    const ViewModels = require('../viewModels/index');

    app.use('/blog', function(req, res) {
        console.log('loading blog postings');
        let viewModel = new ViewModels.BlogViewModel();
        res.render('blog', viewModel); 
    });

    app.use('/', function(req, res) {
        console.log('looking up: ' + req.path);
        let viewModel = new ViewModels.HomeViewModel();
        res.render('home', viewModel);
    });
};
index.js

'use strict';

module.exports = function() {
  const HomeViewModel = require('./home);
  const BlogViewModel = require('./blog);
};
export default class BlogViewModel {
    constructor() {
        this.title = 'My Blog';
    }
}
export default class HomeViewModel {
    constructor() {
        this.title = 'Home';
    }
}
'use strict';
//This module can be used within the class. However, you cannot use it in another file.
const AModule = require('AModule');
//The class name used here just for debug output.
module.exports = class AClass {
  constructor(startValue) {
    //Not like C#. JavaScript does not define private or public.
    this.value = startValue;
  }
  method(incValue) {
    this.value += incValue;
    AModule(); //Just show you can use this module within the class;
  }
}
'use strict';
//You need to put a ./ before the path if you are include another module made by yourself.
//You do not need it for a npm module.
const AClass = require('./AClass.js');
//Now you just required the class of the AClass, you still need to new one;
var aClass = new AClass(500);
//After new one, you can call its method.
aClass.method(30);
//You can access its property by a dot;
console.info(aClass.value); //530
blog.js

'use strict';

module.exports = function() {
  const HomeViewModel = require('./home);
  const BlogViewModel = require('./blog);
};
export default class BlogViewModel {
    constructor() {
        this.title = 'My Blog';
    }
}
export default class HomeViewModel {
    constructor() {
        this.title = 'Home';
    }
}
'use strict';
//This module can be used within the class. However, you cannot use it in another file.
const AModule = require('AModule');
//The class name used here just for debug output.
module.exports = class AClass {
  constructor(startValue) {
    //Not like C#. JavaScript does not define private or public.
    this.value = startValue;
  }
  method(incValue) {
    this.value += incValue;
    AModule(); //Just show you can use this module within the class;
  }
}
'use strict';
//You need to put a ./ before the path if you are include another module made by yourself.
//You do not need it for a npm module.
const AClass = require('./AClass.js');
//Now you just required the class of the AClass, you still need to new one;
var aClass = new AClass(500);
//After new one, you can call its method.
aClass.method(30);
//You can access its property by a dot;
console.info(aClass.value); //530
home.js

'use strict';

module.exports = function() {
  const HomeViewModel = require('./home);
  const BlogViewModel = require('./blog);
};
export default class BlogViewModel {
    constructor() {
        this.title = 'My Blog';
    }
}
export default class HomeViewModel {
    constructor() {
        this.title = 'Home';
    }
}
'use strict';
//This module can be used within the class. However, you cannot use it in another file.
const AModule = require('AModule');
//The class name used here just for debug output.
module.exports = class AClass {
  constructor(startValue) {
    //Not like C#. JavaScript does not define private or public.
    this.value = startValue;
  }
  method(incValue) {
    this.value += incValue;
    AModule(); //Just show you can use this module within the class;
  }
}
'use strict';
//You need to put a ./ before the path if you are include another module made by yourself.
//You do not need it for a npm module.
const AClass = require('./AClass.js');
//Now you just required the class of the AClass, you still need to new one;
var aClass = new AClass(500);
//After new one, you can call its method.
aClass.method(30);
//You can access its property by a dot;
console.info(aClass.value); //530
我的想法是,我可以使用
index.js
来定义我的包或名称空间。然后,在我的路由代码中,我可以这样做:

'use strict';

module.exports = function() {
    const app = this;

    app.use('/blog', function(req, res) {
        console.log('loading blog postings');        
        res.render('blog', {}); 
    });

    app.use('/', function(req, res) {
        console.log('looking up: ' + req.path);
        res.render('home', {});
    });
};
'use strict';

module.exports = function() {
    const app = this;
    const ViewModels = require('../viewModels/index');

    app.use('/blog', function(req, res) {
        console.log('loading blog postings');
        let viewModel = new ViewModels.BlogViewModel();
        res.render('blog', viewModel); 
    });

    app.use('/', function(req, res) {
        console.log('looking up: ' + req.path);
        let viewModel = new ViewModels.HomeViewModel();
        res.render('home', viewModel);
    });
};

但是,当我尝试此操作时,会出现一些运行时错误,如“错误:找不到模块“../viewModels/index”。这意味着我没有正确设置模块。但是,看起来好像是我做错了什么?

您的
index.js
文件不正确,您不能从那里导出ViewModels。将其更改为:

'use strict';

module.exports = {
  HomeViewModel: require('./home'),
  BlogViewModel: require('./blog')
};
而且。。。viewModels它对C#有好处,但对Node.js没有好处。在节点中,它应该只是模型,IMO

更新:


Node.js不完全支持所有ES6功能,尤其是新模块声明:。应使用标准CommonJs模块声明导出函数:

'use strict';

class HomeViewModel {
  constructor() {
    this.title = 'Home';
  }
}

module.exports = HomeViewModel;

事实上,我不确定你想问什么。如果我回答错了,没关系

首先,您得到错误的原因是:找不到模块“../viewModels/index”,因为您在那里放了两个点。应该只有一个点意味着从这里开始。然而,我不确定这是否是问题所在。我想问你把路由代码放在哪里,但我还没有评论的权限。(啊…你在开玩笑吧…)

其次,这里是在ES6中导出类的正确方法

例如:

AClass.js

'use strict';

module.exports = function() {
  const HomeViewModel = require('./home);
  const BlogViewModel = require('./blog);
};
export default class BlogViewModel {
    constructor() {
        this.title = 'My Blog';
    }
}
export default class HomeViewModel {
    constructor() {
        this.title = 'Home';
    }
}
'use strict';
//This module can be used within the class. However, you cannot use it in another file.
const AModule = require('AModule');
//The class name used here just for debug output.
module.exports = class AClass {
  constructor(startValue) {
    //Not like C#. JavaScript does not define private or public.
    this.value = startValue;
  }
  method(incValue) {
    this.value += incValue;
    AModule(); //Just show you can use this module within the class;
  }
}
'use strict';
//You need to put a ./ before the path if you are include another module made by yourself.
//You do not need it for a npm module.
const AClass = require('./AClass.js');
//Now you just required the class of the AClass, you still need to new one;
var aClass = new AClass(500);
//After new one, you can call its method.
aClass.method(30);
//You can access its property by a dot;
console.info(aClass.value); //530
main.js

'use strict';

module.exports = function() {
  const HomeViewModel = require('./home);
  const BlogViewModel = require('./blog);
};
export default class BlogViewModel {
    constructor() {
        this.title = 'My Blog';
    }
}
export default class HomeViewModel {
    constructor() {
        this.title = 'Home';
    }
}
'use strict';
//This module can be used within the class. However, you cannot use it in another file.
const AModule = require('AModule');
//The class name used here just for debug output.
module.exports = class AClass {
  constructor(startValue) {
    //Not like C#. JavaScript does not define private or public.
    this.value = startValue;
  }
  method(incValue) {
    this.value += incValue;
    AModule(); //Just show you can use this module within the class;
  }
}
'use strict';
//You need to put a ./ before the path if you are include another module made by yourself.
//You do not need it for a npm module.
const AClass = require('./AClass.js');
//Now you just required the class of the AClass, you still need to new one;
var aClass = new AClass(500);
//After new one, you can call its method.
aClass.method(30);
//You can access its property by a dot;
console.info(aClass.value); //530
这是在ES6中创建类的100%工作方式

以下是详细的文档:

无论如何,Javascript中的类就像函数一样,函数的
原型将是它的方法
new Class()
只是创建一个对象,使用object作为
this
运行函数类(与
Class.bind(obj,parameters)
相同),然后将新对象的构造函数属性链接到所使用的函数


module.exports=xxx只是将xxx作为该模块的值。例如,如果您
module.exports='Hello'
控制台信息(需要('module'),您将得到Hello

没关系,问题是他没有调用函数,但这也是一个解决方案,即使他会调用函数,他的代码也不会工作。他在一个函数中定义常量,这些常量在函数外部是不可见的。@AlexanderMac当我使用这种方法时,
let viewModel=new ViewModels.HomeViewModel()行抛出
错误:路由:/-ViewModels.HomeViewModel不是一个函数
Node.js不完全支持所有ES6功能,尤其是新模块声明:。使用标准CommonJs模块声明导出函数:
module.exports=HomeViewModel
@AlexanderMac执行
module.exports=HomeViewModel您提到的方法是否替换您答案中的行?或者,是在你答案的最后一个分号之后吗?你不应该将commonjs与ES6模块混合使用。@Bergi-那么,推荐的方法是什么?我正在努力学习这些东西,有很多例子。很难知道什么是好方法,什么是差方法,以及仅仅因为另一个依赖的框架正在使用的方法而做了什么。我建议您使用
index.js