P
P
prosto_anton2014-04-27 09:45:47
JavaScript
prosto_anton, 2014-04-27 09:45:47

Why are function properties not visible in nodejs class constructor?

There is a type constructor:

"use strict"; // не влияет на возникновение ошибки
function Make() {
     this.aaa = function () {};  // любая функция с именем или без
}
exports.Make = Make;  // Экспортирую конструктор

Next, an instance is created:
var elem = new Make();
The problem is that nodejs considers the elem.aaa property to be undefined when the script is executed in normal mode and in the built-in debugger, but when I run the nodejs interpreter in the terminal and include the file with this constructor, and then I create an instance:
a = require("./Make.js");  // добавляю var -- получаю undefined
    b = new a.Make();  // добавляю var -- получаю undefined

then this instance has the aaa property and works correctly.
PS: I've been trying to figure out what's going on for 3 days, sorry if this is a dumb question.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
fart, 2014-04-27
@prosto_anton

> var mk=require("./Make.js");
undefined
> var elem = new mk.Make();
undefined
> console.log(typeof elem.aaa);
function
undefined
> elem.aaa();
undefined

.............. ПЕРЕОПРЕДЕЛИЛ:  this.aaa = function () { return "OK"; }

> var mk=require("./Make.js");
undefined
> var elem = new mk.Make();
undefined
> console.log(typeof elem.aaa);
function
undefined
> elem.aaa();
'OK'

undefined is the return of the last command executed in REPL mode, note console.log also returned undefined. i.e. in the first case, elem.aaa() returned undefined. this.aaa = function(){}; really doesn't return anything. But at the same time, js sees the elem.aaa function expression.
typeof elem.aaa === 'function'
Returning undefined node in REPL mode can be modified

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question