Javascript store function with parameters in variable

Possible Duplicate:
Javascript infamous Loop problem?

I have the following:

function test[a,b,c]{
    console.log[a+b+c];
}
for [var i=0; i < array.length; i++]{
    steps.push[
        function[]{ test[array[i].something, array[i].wow, i];}
    ];

I want to store functions with several parameters, and bind them to buttons later when the DOM is loaded

for [var i=0; i= 10" is the exit condition [equivalent to "![x < 10]"]
  if [x >= 10] {
    return;
  }
  // do stuff
  loop[x + 1]; // the recursive call
}
loop[0];

However, some algorithms cannot be simple iterative loops. For example, getting all the nodes of a tree structure [such as the DOM] is easier via recursion:

function walkTree[node] {
  if [node === null] {
    return;
  }
  // do something with node
  for [let i = 0; i  syntax in future JavaScript] has a shorter syntax compared to
function expressions and does not have its own this, arguments, super, or
new.target. Arrow functions are always anonymous.

Two factors influenced the introduction of arrow functions: shorter functions and non-binding of this.

Shorter functions

In some functional patterns, shorter functions are welcome. Compare:

const a = [
  'Hydrogen',
  'Helium',
  'Lithium',
  'Beryllium'
];

const a2 = a.map[function[s] { return s.length; }];

console.log[a2]; // logs [8, 6, 7, 9]

const a3 = a.map[[s] => s.length];

console.log[a3]; // logs [8, 6, 7, 9]

No separate this

Until arrow functions, every new function defined its own this value [a new object in the case of a constructor, undefined in strict mode function calls, the base object if the function is called as an "object method", etc.]. This proved to be less than ideal with an object-oriented style of programming.

function Person[] {
  // The Person[] constructor defines `this` as itself.
  this.age = 0;

  setInterval[function growUp[] {
    // In nonstrict mode, the growUp[] function defines `this`
    // as the global object, which is different from the `this`
    // defined by the Person[] constructor.
    this.age++;
  }, 1000];
}

const p = new Person[];

In ECMAScript 3/5, this issue was fixed by assigning the value in this to a variable that could be closed over.

function Person[] {
  const self = this; // Some choose `that` instead of `self`.
                     // Choose one and be consistent.
  self.age = 0;

  setInterval[function growUp[] {
    // The callback refers to the `self` variable of which
    // the value is the expected object.
    self.age++;
  }, 1000];
}

Alternatively, a bound function could be created so that the proper this value would be passed to the growUp[] function.

An arrow function does not have its own this; the this value of the enclosing execution context is used. Thus, in the following code, the this within the function that is passed to setInterval has the same value as this in the enclosing function:

function Person[] {
  this.age = 0;

  setInterval[[] => {
    this.age++; // `this` properly refers to the person object
  }, 1000];
}

const p = new Person[];

Predefined functions

JavaScript has several top-level, built-in functions:

eval[]

The eval[] method evaluates JavaScript code represented as a string.

isFinite[]

The global isFinite[] function determines whether the passed value is a finite number. If needed, the parameter is first converted to a number.

isNaN[]

The isNaN[] function determines whether a value is NaN or not. Note: coercion inside the isNaN function has interesting rules; you may alternatively want to use Number.isNaN[] to determine if the value is Not-A-Number.

parseFloat[]

The parseFloat[] function parses a string argument and returns a floating point number.

parseInt[]

The parseInt[] function parses a string argument and returns an integer of the specified radix [the base in mathematical numeral systems].

decodeURI[]

The decodeURI[] function decodes a Uniform Resource Identifier [URI] previously created by encodeURI or by a similar routine.

decodeURIComponent[]

The decodeURIComponent[] method decodes a Uniform Resource Identifier [URI] component previously created by encodeURIComponent or by a similar routine.

encodeURI[]

The encodeURI[] method encodes a Uniform Resource Identifier [URI] by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character [will only be four escape sequences for characters composed of two "surrogate" characters].

encodeURIComponent[]

The encodeURIComponent[] method encodes a Uniform Resource Identifier [URI] component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character [will only be four escape sequences for characters composed of two "surrogate" characters].

escape[]

The deprecated escape[] method computes a new string in which certain characters have been replaced by a hexadecimal escape sequence. Use encodeURI or encodeURIComponent instead.

unescape[]

The deprecated unescape[] method computes a new string in which hexadecimal escape sequences are replaced with the character that it represents. The escape sequences might be introduced by a function like escape. Because unescape[] is deprecated, use decodeURI[] or decodeURIComponent instead.

  • « Previous
  • Next »

Can I store a function in a variable JavaScript?

In JavaScript, functions are called Function Objects because they are objects. Just like objects, functions have properties and methods, they can be stored in a variable or an array, and be passed as arguments to other functions.

Can a JavaScript function have parameters?

A JavaScript function can have any number of parameters. The 3 functions above were called with the same number of arguments as the number of parameters. But you can call a function with fewer arguments than the number of parameters.

How do you put a function into a variable?

we put the function in a variable if inside the function block we use the return method: var multiplyTwo = function [a] { return a * 2; };

Can JavaScript functions have properties?

In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called.

Bài Viết Liên Quan

Toplist mới

Bài mới nhất

Chủ Đề