开发者

javascript unique(?) function writing

开发者 https://www.devze.com 2023-04-12 17:12 出处:网络
a lot of times i can see function written like this : name : function(){ ... } i got several function about this :

a lot of times i can see function written like this :

name : function(){ ... }

i got several function about this :

  1. what is this function different from normal(?) functions开发者_Python百科
  2. what this kind of function calling like?

i couldn't find any information about this kind of function , tutorial will help.


A function in the format:

name: function() { ... }

Is a function within an object literal.

obj = {
  myfunction: function() { ... }
}

Which can be accessed by obj.myfunction();


It's called an anonymous function.

It's different than a normal function because:

  1. It's not global
  2. It has access to variables in the outer scope

Calling one is just like normal:

var a = function(input) {
    alert(input);
};
a();

Or just..

function(input) {
    alert(input);
}();

And access the outer scope:

var output = "something";
function() {
    alert(output);
}();

They're frequently used as callbacks, where you pass one function to another, and that function calls your function. For example, here's some code to wait 1 second and then pop up with "Done waiting!":

setTimeout(function() {
    alert("Done waiting!");
}, 1000);


There is some code missing. It should look like this:

var myobject = {name : function(){ ... }};
  1. It's part of an object. In this case it belongs to the object myobject.
  2. To call this function you have to write myobject.name()


These are called "function expressions" when defined like this, the "this" property inside the function points the the object this function is defined against (ie. the object holding "name" property).

doing something like

function name () {...}

Is called a function declaration and the "this" property points to the global object.


That's a function just as normal as any. The only difference is that it is stored somewhere, probably in a regular old object.

You can make objects in JavaScript:

var blah = {}; // empty

Or maybe like this, storing key/value pairs:

var point = {x: 5, y: 22};

Then you can do:

point.x; // is 5!

An easier-to-look-at way of writing the same thing is:

var point = {
  x: 5,
  y: 22
};

There you can very clearly see that x is "mapped" to 5 and y is "mapped" to 22.

In this case, instead of making the value "5" or "22", the value is a function

var point = {
  x: 5,
  y: 22,
  functionName: function() { ... }
};

Then you can use that object like this:

point.x; // is 5!
point.functionName(); // calls the function!

A lot of times in JavaScript you will see basically this:

var LibraryOfFunctions = {
  functionA: function() { ... },
  functionB: function() { ... },
  functionC: function() { ... },
};


a function without a name in the front is an anonymous function. You can assign an anonymous function to a variable:

 var myfunc = function {alert('hi')};

then call it like:

 myfunc();

Or it can be defined as part of a hash:

 var myhash = {}  // empty hash
 var myhash = {my_int: 5} //hash with a key and int value
 var myhash = {myfunc: function() {alert('hi');}}  //hash with key and function as a value

Now you can all it like:

myhash.myfunc()

You see this usage alot in jQuery configurations where a list of options is passed as a hash, some of them with anonymous functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号