JavaScript for C#Developers Study(2)

JavaScript Functions

 

  • Not like c#, less parameter(s) can be passed to a function, the omitted one(s) would be undefined

image

  • There isn’t method overloading like c#, the latter definition simply makes the former one disappear.

image

  • arguments object can be used within a function to check the parameters

image

  • Using the arguments object to access the values

image

 

  • Return Values
    • if there isn’t return in the function, or the return expression doesn’t return anything, the return value of the function is undefined.

image

 

  • Function object itself has its own properties and member functions

image

  • Anonymous Function

image

 

  • The “this” keyword in a function
    • “this” applies to the run time owner of the function

 image

image

    • Details of the “this” keyword, get more HERE

image

    • bind() lets you change the owner of a function

image

image

JavaScript function invocation under the hood

Functions, like pretty much everything else in JavaScript, are objects: each function is an instance of the core Function object and thus inherits a method named call which invokes the code defined within the function body. The call method expects at least one argument which is the object that will act as the value of this within the particular execution of the function: any further arguments are passed along as arguments for the function body.

1 function greet(name) {
2     alert("Hi " + name + ", my name is " + this.name + ".");
3 }
4 
5 var me = {
6     name: "Nathaniel"
7 };
8 
9 greet.call(me, "John"); // "Hi John, my name is Nathaniel."

Of course, it’s unlikely that you see this particular style of function invocation as frequently as you see the more common identifier(arg1, arg2, ..., argN) syntax, but in reality that’s nothing more than a convenience syntax offered by the language so that developers don’t have to use the call method over and over again.

1 function greet(name) {
2     alert("Hi " + name + ", my name is " + this.name + ".");
3 }
4 
5 // From JavaScript's perspective this...
6 greet("Jane");
7 
8 // ...is actually this.
9 greet.call(window, "Jane");

Note that when we make a baseless function call, e.g. greet("Jane"), JavaScript automatically passes in the global context (the window object) as the first argument of the implicit call method so any function called in this manner uses the global context.

Another common way to call a function is to invoke it as the method of an object in which case JavaScript determines that the object should be passed as the context argument.

 1 function greet(name) {
 2     alert("Hi " + name + ", my name is " + this.name + ".");
 3 }
 4 
 5 me = {
 6     name: "Nathaniel",
 7     greet: greet
 8 };
 9 
10 // From JavaScript's perspective this...
11 me.greet("Jeff");
12 
13 // ...is actually this.
14 greet.call(me, "Jeff");

Read more HERE

Examples of baseless function invocation and self invocation:

 1 var a = {
 2     b: function() {
 3         return this;
 4     }
 5 };
 6 
 7 var foo = a.b;
 8 foo(); //window
 9 
10 var a = {
11     b: function() {
12         var c = function() {
13             return this;
14         };
15         return c();  // baseless invocation
16     }
17 };
18 
19 a.b(); //window
1 var a = {
2     b: function() {
3         return (function() {return this;})(); // self invoking
4     }
5 };
6 
7 a.b(); //window

Read more HERE

  • Closures
    • References outside a function are accessible in function, regardless of lifetime

image

 

  • Scoping
    • In c#, Curly Braces make a scope, variables defined inside won't live outside

image

    • In JavaScript, Curly Braces DON’T make a scope, that means variables defined inside still live outside

image

    • The ONLY thing creates a scope in JavaScript is Function

image

 

  • Namespace
    • Polluting the Global Scope
      • Name Collision Problematic in Large Projects

image

 

    • Create an object as Namespace

image

    • Namespaces and Anonymous Self-Executing Function handles the global pollution

image

posted @ 2012-10-19 04:51  Jian, Li  Views(296)  Comments(0)    收藏  举报