[英]Why is a variable assigned 'this' in module.export function in javascript
I am trying to understand the following code taken from a service created in a feathersjs app. 我试图理解以下代码,这些代码取自于feathersjs应用程序中创建的服务。
// Initializes the `users` service on path `/users`
const createService = require('feathers-knex');
const createModel = require('../../models/users.model');
const hooks = require('./users.hooks');
const filters = require('./users.filters');
module.exports = function () {
const app = this;
const Model = createModel(app);
const paginate = app.get('paginate');
const options = {
name: 'users',
Model,
paginate
};
// Initialize our service with any options it requires
app.use('/users', createService(options));
// Get our initialized service so that we can register hooks and filters
const service = app.service('users');
service.hooks(hooks);
if (service.filter) {
service.filter(filters);
}
};
This file is then imported as follows: 然后按以下方式导入该文件:
// many requires..
const feathers = require('feathers');
// more requires
const services = require('./services');
// other requires
const app = feathers();
Can someone explain as to what does the line 有人可以解释一下线路是什么
const app = this
do in the code that is creating the service? 在创建服务的代码中做什么?
It assigns the value of this
to a variable (well, a constant) with a name that more clearly describes the value than this does. 它的值赋给
this
给一个变量(当然,常数)与更清楚地描述了比这确实值的名称。 It is designed to make the code easier for maintainers to understand. 它旨在使维护人员更容易理解代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.