Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I declare a variable or access object method? [closed]

Which is the better practice and why?

Declare a variable:

exampleFunction(requestData: Object) {
  const username = requestData.username;

  doSomething(username);
}

Or access the object property directly?

exampleFunction(requestData: Object) {
  doSomething(requestData.username);
}

What if the code I use is over 50 lines long and the variable is used multiple times. Should I use the variable 'username' multiple times or use 'requestData.username' multiple times?

like image 388
Jim L Avatar asked Jan 08 '19 13:01

Jim L


People also ask

What are the disadvantages of using closures?

Disadvantages of closures There are two main disadvantages of overusing closures: The variables declared inside a closure are not garbage collected. Too many closures can slow down your application. This is actually caused by duplication of code in the memory.

When should closures be used?

Closures are frequently used in JavaScript for object data privacy, in event handlers and callback functions, and in partial applications, currying, and other functional programming patterns.

Which is correct way to access method by the object?

To access the object, a method can use the this keyword. The value of this is the object “before dot”, the one used to call the method.

Are closures useful?

Closures are useful because they let you associate data (the lexical environment) with a function that operates on that data. This has obvious parallels to object-oriented programming, where objects allow you to associate data (the object's properties) with one or more methods.

Why should I declare an object variable as a class?

Declaring an object variable as a specific class gives you several advantages: When Option Strict is turned On, an object variable can access only the methods and properties of the class with which you declare it. The following example illustrates this.

How do you declare an object variable in a procedure?

You can declare an object variable with the Object data type when the specific object type is not known until the procedure runs. Use the Object data type to create a generic reference to any object. If you know the specific object type, you should declare the object variable as that object type.

How to treat an object variable as an object?

You can treat an object variable exactly the same as the object to which it refers. You can set or return the properties of the object or use any of its methods. Declare the object variable.

How do you assign an object to a variable in Visual Basic?

How to: Declare an Object Variable and Assign an Object to It in Visual Basic. You declare a variable of the Object Data Type by specifying As Object in a Dim Statement. You assign an object to such a variable by placing the object after the equal sign (=) in an assignment statement or initialization clause.


1 Answers

V8 Javascript Engine performs some optimization pre-processing the code and checking if some variable can be deleted or if there's some regular pattern in the code, in order to reduce the execution payload.

For this reason, there are no memory issues in using a form or another, at least using V8 engine (for example, Chrome or NodeJS).

Even if this optimization isn't performed, it's just a pointer or a primitive variable, so it won't cost more than few bytes.

It's different if we talk about the cost of transmitting code. We know that JavaScript isn't a compiled language and it needs to reach the client's computer to be executed, so every space, indentation, semicolon, will cost in order of time used to transfer.

To reduce this cost, you can uglify and minify your code, but usually this type of optimization isn't performed.

Finally, it's impossible to think a scenario where it can make a difference, so I advise you to use the form that you think it's more readable and no matter of other parameters.

like image 95
Cristian Traìna Avatar answered Sep 29 '22 11:09

Cristian Traìna