简体   繁体   English

Javascript对象函数返回null

[英]Javascript object function is returning null

I'm trying to do a very simple OOP in Javascript (Node.js) but having issues. 我正在尝试用Javascript(Node.js)做一个非常简单的OOP,但是有问题。 I tried everything already, including searching, but did not find an answer. 我已经尝试了所有方法,包括搜索,但没有找到答案。

Basically, I have this file Test.js: 基本上,我有以下文件Test.js:

class Test {

constructor(){
    this.name = 'Hey';
    this.config = 'null!';
    console.log('this.config: ' + this.config);
}

config(msg){
    this.config = msg;
    console.log('new this.config: ' + this.config);
}

}

module.exports = Test;

(I also tried this:) (我也尝试过这个:)

function Test()
{
    this.name = 'Hey';
    this.config = 'null!';
    console.log('this.config: ' + this.config);
}

Test.config = function(msg) // and Test.prototype.config
{
    this.config = msg;
    console.log('new this.config: ' + this.config);
}

module.exports = Test;

And I have this other file app.js: 我还有另一个文件app.js:

var TestModule = require('./Test.js');
var Test = new TestModule();
var test = Test.config('hi');

Other way I've tried: 我尝试过的其他方式:

var TestModule = require('./Test.js');
var Test = new TestModule().config('hi');

and also did not work. 而且也没有用。

I tried many different things already, but no matter what, when I try to run the config function in the same instance, the object turns null... does anyone know why that happens? 我已经尝试了许多不同的方法,但是无论如何,当我尝试在同一实例中运行config函数时,对象变为null ...有人知道为什么会发生这种情况吗? Maybe I'm missing something really obvious. 也许我缺少真正明显的东西。

You are assigning your var Test to be the return value of the config function. 您正在将var Test分配为config函数的return值。

var test = Test.config('hi!');

As config does not return anything, this will result in test being null. 由于config不返回任何内容,因此将导致test为空。

You should either make your config method return something (this would be a form of the "method chaining" design pattern ), or simply not assign the result of the config call to a variable. 您应该使config方法返回某些内容(这将是“方法链”设计模式的一种形式),或者干脆不将config调用的结果分配给变量。

For example, you could simply do this: 例如,您可以简单地执行以下操作:

var test = new TestModule();
test.config('hi!');
// the 'test' variable still contains a reference to your test module

You first snippet is correct 你的第一段是正确的

class Test {

    constructor() {
      this.name = 'Hey';
      this.config = 'null!';
      console.log('this.config: ' + this.config);
    }

    config(msg) {
      this.config = msg;
      console.log('new this.config: ' + this.config);
    }

  }

  module.exports = Test;

config is an instance method not a class method or static method. config是实例方法,不是类方法或静态方法。

You need to call config() on a Test instance. 您需要在测试实例上调用config() like 喜欢

var Test = require('./Test.js');
var testObj = new Test();

Now testObj is instance and you can call config() method on this object. 现在testObj是实例,您可以在此对象上调用config()方法。

test.config('Hi');

It will print/log a message but it will not return any thing but undefined because you are not returning anything from that method. 它会打印/记录一条消息,但它不会返回任何东西,但会返回undefined因为您没有从该方法返回任何信息。

I hope this explains the problem. 我希望这可以解释问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM