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
- There isn’t method overloading like c#, the latter definition simply makes the former one disappear.
- arguments object can be used within a function to check the parameters
- Using the arguments object to access the values
- 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.
- Function object itself has its own properties and member functions
- Anonymous Function
- The “this” keyword in a function
- “this” applies to the run time owner of the function
-
- Details of the “this” keyword, get more HERE
-
- bind() lets you change the owner of a function
JavaScript function invocation under the hood
Functions, like pretty much everything else in JavaScript, are objects: each function is an instance of the core
Functionobject and thus inherits a method namedcallwhich invokes the code defined within the function body. Thecallmethod expects at least one argument which is the object that will act as the value ofthiswithin 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 thecallmethod 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 (thewindowobject) as the first argument of the implicitcallmethod 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(); //window1 var a = { 2 b: function() { 3 return (function() {return this;})(); // self invoking 4 } 5 }; 6 7 a.b(); //windowRead more HERE
- Closures
- References outside a function are accessible in function, regardless of lifetime
- Scoping
- In c#, Curly Braces make a scope, variables defined inside won't live outside
-
- In JavaScript, Curly Braces DON’T make a scope, that means variables defined inside still live outside
-
- The ONLY thing creates a scope in JavaScript is Function
- Namespace
- Polluting the Global Scope
- Name Collision Problematic in Large Projects
- Polluting the Global Scope
-
- Create an object as Namespace
-
- Namespaces and Anonymous Self-Executing Function handles the global pollution




















浙公网安备 33010602011771号