JavaScript Tips & Tricks

Introduction

While writing some JavaScript code in your RPG Maker MV plugins, web applications, and whatever, there are somewhat small things you're ignoring. They may not often affect your script performance and results, but practically you should pay attention on them to write more efficient code and avoid unwanted effects. Here some useful tips for such those.

Tip #1: Avoid global variables, always declare local variables

Minimize the use of global variables. This includes all data types, objects, and functions. Global variables and functions can be overwritten by other scripts.

All variables used in a function/scope should be declared as local variables. Local variables must be declared with the var keyword, otherwise they will become global variables.

Tip #2: Declarations on top

It is a good coding practice to put all declarations at the top of each script or function. This will:

  • Give cleaner code
  • Provide a single place to look for local variables
  • Make it easier to avoid unwanted (implied) global variables
  • Reduce the possibility of unwanted re-declarations

Tip #3: Initialize variables

It is a good coding practice to initialize variables when you declare them. This will:

  • Give cleaner code
  • Provide a single place to initialize variables
  • Avoid undefined values

var firstName = "",
lastName = "",
price = 0,
discount = 0,
fullPrice = 0,
myArray = [],
myObject = {};

Tip #4: Never declare Number, String, or Boolean objects

Declaring these types as objects, slows down execution speed, and produces nasty side effects.

  • Use {} instead of new Object()
  • Use "" instead of new String()
  • Use 0 instead of new Number()
  • Use false instead of new Boolean()
  • Use [] instead of new Array()
  • Use /()/ instead of new RegExp()
  • Use function (){} instead of new function()

Tip #5: Beware of automatic type conversions

Beware that numbers can accidentally be converted to strings or NaN (Not a Number). JavaScript is loosely typed. A variable can contain different data types, and a variable can change its data type:

var x = 5 + "7"; // x is "57", typeof x is a string

Tip #6: Use === comparison

The == comparison operator always converts (to matching types) before comparison. The === operator forces comparison of values and type.

0 == ""; // true
1 == "1"; // true
1 == true; // true
0 === ""; // false
1 === "1"; // false
1 === true; // false

Tip #7: Use Parameter Defaults

If a function is called with a missing argument, the value of the missing argument is set to undefined. Undefined values can break your code. It is a good habit to assign default values to arguments.

function myFunction(x, y) {
if (y === undefined) {
y = 0;
}
}

or shorter one...

function myFunction(x, y) {
y = y || 0;
}

Tip #8: End Your Switches with Defaults

Always end your switch statements with a default. Even if you think there is no need for it.

Tip #9: Avoid Using eval()

The eval() function is used to run text as code. In almost all cases, it should not be necessary to use it. Because it allows arbitrary code to be run, it also represents a security problem.

Source

w3schools.com

3 comments: