Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我是否必须使用;这一点;与“合作”;“对象”;NodeJs中的属性?_Javascript_Node.js - Fatal编程技术网

Javascript 我是否必须使用;这一点;与“合作”;“对象”;NodeJs中的属性?

Javascript 我是否必须使用;这一点;与“合作”;“对象”;NodeJs中的属性?,javascript,node.js,Javascript,Node.js,我正在从Java切换到NodeJs,所以有些事情对我来说仍然很模糊 我正在尝试使用脚本,就像在Java中使用类一样。我知道这是一种方法: var client = require('scp2'); var host, username, password; var SftpHandler = function (host, username, password) { this.host = host; this.username = username; this.password

我正在从Java切换到NodeJs,所以有些事情对我来说仍然很模糊

我正在尝试使用脚本,就像在Java中使用类一样。我知道这是一种方法:

var client = require('scp2');
var host, username, password;

var SftpHandler = function (host, username, password) {
  this.host = host;
  this.username = username;
  this.password = password;
 };



SftpHandler.prototype.downloadFile = function (path, callback) {
    console.log(this.host,username,password,path);
};

module.exports = SftpHandler;
问题是当我从另一个脚本调用它时,如下所示:

var sftpHandler = new SftpHandler(credentials.sftpHost, credentials.sftpUsername, credentials.sftpPassword);
sftpHandler.downloadFile(credentials.sftpPathToImportFiles+configFile.importFileName, callback);
我在控制台日志中有
162.*.*未定义未定义未定义…


我意识到这是因为我所指的对象属性中缺少
这个。
。但是为什么需要这个。?这是正确的方法吗?

有时在Java和JavaScript之间映射概念有点棘手

简单的回答是,是的,如果您要尝试在JavaScript中创建类似于Java中的类实例的东西,那么每次都需要调用“this”

原因是JS中的
this
实际上是一个特殊的局部范围变量,位于引用调用范围的函数中。信不信由你,它并不总是实际的类实例,所以你需要更彻底地阅读它

许多人选择不去尝试,因为在很多地方,遵循Java习惯用法要么会给您带来麻烦,要么只会让事情变得更难/更复杂/更难维护

但是在任何情况下,如果您的downloadFile方法需要提供嵌套函数,例如,您希望使用匿名函数处理回调,那么作为
可以如何更改的示例:

SftpHandler.prototype.downloadFile = function (path, callback) {
    console.log(this.host); // this here refers to your object instance
    fileManager.getFile(path, function(e, file){
       console.log(this.host); // 'this' here refers to the anonymous function, not your object
    })
};
希望有帮助

**编辑如何在回调中访问此**

有几种方法可以维护引用。通常只需将范围中的另一个变量设置为原始的
this
,如下所示:

SftpHandler.prototype.downloadFile = function (path, callback) {
    console.log(this.host); // this here refers to your object instance
    var self = this;

    fileManager.getFile(path, function(e, file){
       console.log(this.host); // 'this' here refers to the anonymous function, not your object
       console.log(self.host); // 'self' there refers to the enclosing method's 'this'
    })
};
其他人则更倾向于使用函数#bind:


要避免使用模块可以是一个返回对象的函数:

var client = require('scp2');

var SftpHandler = function (host, username, password) {

  return {
    downloadFile: downloadFile
  };

  function downloadFile (path, callback) {
    console.log(host, username, password,path);
  }
};

module.exports = SftpHandler;
然后调用此函数而不使用new

var sftpHandler = SftpHandler(credentials.sftpHost, credentials.sftpUsername, credentials.sftpPassword);
sftpHandler.downloadFile(credentials.sftpPathToImportFiles+configFile.importFileName, callback);

我明白了,保罗,谢谢。正如您编写的示例中所述,
嵌套函数中的此
引用其自己的参数。那么,您通常如何从该函数中访问实例/对象属性呢+1 B我就是这么想的。但我认为这是一个肮脏的解决方案。谢谢你的推荐。
var sftpHandler = SftpHandler(credentials.sftpHost, credentials.sftpUsername, credentials.sftpPassword);
sftpHandler.downloadFile(credentials.sftpPathToImportFiles+configFile.importFileName, callback);