Java Script for C developers Dhananjay Kumar debugmode

Java. Script for C# developers Dhananjay Kumar @debug_mode Microsoft MVP http: //debugmode. net

Agenda ü ü ü Function as statement Function as expression Return statement Parameters in function The Arguments object Vargs Java. Script function Scopes in functions – global, local, and lexical this keyword Java. Script objects Object as literal Object using new operator or constructor pattern Object using static method Object. Create()

I am Dhananjay Kumar ü 6 times Microsoft MVP ü Developer Evangelist at Infragistics ü DKumar@infragistics. com ü @debug_mode ü Blog : http: //debugmode. net

Share your experience by tweeting and Facebook with hashtag #Infragistics

We will learn everything by writing codes

Java. Script functions Function expression ü A function expression creates an instance of the function object ü It can be passed as an argument to a function ü it can be returned from a function ü it can be assigned to a variable or an array ü Function created as an expression cannot be executed before it is defined Function statement ü It is must to give a name to function statement ü For function statement, Java. Script creates an object and assigns that to a variable ü Function created as statement are hoisted at the top, hence can be used before its defined

Java. Script functions : Return statement ü A Java. Script function may or may not have a return statement ü By default Java. Script function returns undefined ü Java. Script function can return a value or expression ü If there is no expression with return statement, Java. Script function returns undefined In case of Java. Script constructor function, by default function returns the invoking object instead of undefined

Java. Script functions : parameters ü Java. Script does not do type checking of the parameters in function ü When less parameters are passed, non passed parameter would be undefined ü Java. Script does not check the number of parameters being passed to function ü When more parameters are passed, extra parameters can be read using arguments object

Java. Script functions : arguments object ü Along with parameters , arguments object also get passed to a Java. Script function. ü The arguments is an array like object, however it is not a pure array. ü Number of passed parameters can be find using arguments. length ü When more parameters are passed, extra parameters can be read in the arguments object ü Value of arguments object can be read using the indexes. ü Java. Script array push and pop operation cant be performed on arguments object

Java. Script functions : Varargs function ü Varargs functions are those functions which work on any number of parameters ü Using the arguments object, all the passed parameters can be iterated and used

Java. Script functions : scopes ü Scoping stands for the current context of code execution ü A variable or expression can be executed in the context of the function or in the global context of Java. Script ü Java. Script does not have block scoping ü temp can only be accessed inside function foo ü temp is created using the function scope ü temp can’t be accessed outside function foo

Java. Script functions : scopes ü A variable which is not defined inside any function is in global scope. ü Variable in the global scope can be used anywhere in the program ü Variable inside a function works in local scope. ü Variable in the local scope are accessible only inside the function global Scopes Functional/local lexical

Java. Script functions : lexical scope ü Lexical scope works with the nested functions. ü Lexical scoping means that anything (function, object, variable) defined in the parent scope can be accessed inside the child scope. However the opposite is not true. ü Let’s say there is a function “B” ü Function B is nested inside function A; which is inside function “A”. The ü The variable named my. Var is created in the outer function A, so inner function B has access to the can be accessed inside the child function B; ü The variable local. Var is created inside the inner function B, so scope of the outer function A is cannot be accessed inside the outer function A; lexical scoping

Java. Script functions : this keyword ü “this” represents the calling context ü in the global scope “this” represents either a document or window object. ü In the function scope (or inside the function), the value of “this” is determined by how the function is being called ü In the strict mode, if we don’t set the calling base then the value of “this” would be set to undefined in the browsers. ü Value of “this” is set to object calling the function

Java. Script objects ü Objects can be created in three possible ways. Objects as literal ü Object as literal ü Creating an object using the new operator and constructors ü Creating an object using the Object. create() static method Objects Object using function constructor or new operator Object using Object. Create() static method


Summary • • • Function as statement Function as expression Return statement Parameters in function The Arguments object Vargs Java. Script function Nested functions Scopes in functions this keyword Function as constructor Callback function The call() and the apply() methods

What Infragistics can offer you? • We welcome all of you to take advantage of a FREE 30 Day Trial by downloading the product at: http: //www. infragistics. com/products/ultimate/download • Reach me at DKumar@infragistics. com • Please reach out to us at Sales-India@infragistics. com for any follow up questions you may have. We welcome the opportunity to assist you.
- Slides: 18