What is happening here in this code? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can someone tell me what is happening here?I only know that it is requiring some core modules.
var inherits = require('util').inherits;
var express = require('express')();

The util module has exported an object that contains (probably amongst others) a function under the key inherits:
exports = {
inherits: function() ...
}
The express module on the other hand, has directly exported a whole function, and that function is immediately invoked and the result assigned to the variable express.
module.exports = exports = function() {
return ...
}
It is likely that the function has also returned an object containing key/value pairs of functions, just like you'd get from a normal exports object.
See also What is the purpose of Node.js module.exports and how do you use it?

Related

Access array from other function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
Hello i'm trying to access an array from a function what i'am returning work, it's an array but the result in my function "onChangeClient" always display the same "undefined". why ?
Typescript:
JavaScript (and hence TypeScript) uses lexical scope, meaning you cannot create an Array in an inner function and access it from outside. What you can do: Create the array in the outer function (getRessource) and return it from there.
async getRessource(id) {
var ref = ... elided ...
const returnArray = []; // define it here
await ref.once('value' ... elided ... );
return returnArray;
}

What is "this.return" in JavaScript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
While in VS Code, I hit the tab after typing return and the VS Code Intellisense auto-completed "return" to "this.return".
I've never seen this before and cannot find any documentation on it.
Does anybody know if this is actually a thing, or if VS Code possibly lost it's marbles.
Note: the object I was working in does not have any properties or functions called "return".
Well, an object could have a property called return:
const obj = {
return: () => { console.log('↩️'); }
};
obj.return();
Were you in the context of a class that had a return property, or maybe its superclass did?
(Honestly it seems more likely VS Code was just being weird.)
You can run console.log("this", this) in most modern browsers this will return the JSON data of this (the variable this refers to the current instance of declared object FYI), and console.log(typeof(this.return)) and it will likely return function (if you get undefined just change it from this.return to return;)
Likely the object either has a property called return that is a function, or something has gone wrong in the autocomplete.

What is the difference between these ways when creating a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
When creating functions, What is the main difference and when to use these ways of creating a function?
onSubmit = () => {}
functionsName : () => {}
There's no difference in the creation of the functions, just what is done with the function once it's created:
Your onSubmit one creates a function and assigns it to an in-scope variable.
Your functionsName one creates a function and assigns it to an object property. This form is only valid within an object initializer. (Outside of an object initializer, it's not a syntax error, but it's just a labelled statement and the function is never assigned to anything.)
You may find another answer of mine useful as well. It's a rundown of various ways of creating functions.

Is there a point to adding getter methods in standard ES 6 classes? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I find myself doing stuff like this subconsciously because I usually write PHP.
class Car {
constructor(engine) {
this.engine = engine;
}
getEngine() {
return this.engine;
}
}
But only recently did I realise there might be no real practical point to doing so since there are no access modifiers in javascript anyways; in other words, might as well do car.engine instead of car.getEngine()
follow-up
If there is a point; would you (unit) test the getter method (if so, how) and why/why not?
No, methods like getEngine are unnecessary clutter. Just access your properties directly, e.g. as car.engine. In JavaScript, all properties are public for a reason.
Should you ever need to add logic to the getter method, or refactor the class so that the engine is not directly accessible any more, you can still (and should) just use a transparent getter property:
class Car {
constructor(engine) {
this._engine = engine;
}
get engine() {
return this._engine; // or anything else
}
}

Is there any catch/gotcha in this JavaScript boilerplate? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm developing a website with jQuery and organize code into several script files.
If I didn't get it wrong, there's a best practice of organizing your code like this boilerplate (which creates a separated scope and set the undefined value correctly):
(function($, undefined) {
$(document).ready(function() {
// code here
});
})(jQuery);
In my case, I will also rely on some external configuration outputted by some server-side script, say it is stored as global variable site_config. So currently my script boilerplate is:
(function(window, undefined) {
var document = window.document;
var $ = window.jQuery;
var config = window.site_config;
// equivalent to $(document).ready(function() {...
$(function() {
// code here using `config`
});
})(window);
So my question is: is there any catch/gotcha in this boilerplate?
Specifically:
is it OK to pass in window explicitly? (suppose the wrapper function is in global scope)
should I use window.document or jQuery.document? If both are wrong, how to reference correctly?
is it OK to pass in window explicitly?
It's OK, but unnecessary (see Why do I pass the window object in to the module pattern?).
should I use window.document or jQuery.document? If both are wrong, how to reference correctly?
jQuery.document is wrong, the jQuery function object has no such property. However, it would be fine to just use document, as it is a global and non-writable variable.

Categories

Resources