Date post: | 01-Jul-2015 |
Category: |
Documents |
Upload: | salvatore-fazio |
View: | 216 times |
Download: | 1 times |
1 | SharePoint Saturday Milan – 18 May 2013
2 | SharePoint Saturday Milan – 18 May 20132 | SharePoint Saturday Milan – 18 May 2013
Grazie ai nostri Sponsor!
3 | SharePoint Saturday Milan – 18 May 2013
Salvatore Di Fazio – Technical Architect – Content and Code - [email protected] - @Salvodif – MVP SharePoint Server http://salvatoredifaziosharepoint.blogspot.com
Javascript Patterns
4 | SharePoint Saturday Milan – 18 May 20134 | SharePoint Saturday Milan – 18 May 2013
Horizontal session
5 | SharePoint Saturday Milan – 18 May 20135 | SharePoint Saturday Milan – 18 May 2013
Starter JavaScript is object based language Variables created using var You should always use semicolons, and an object is a collection of
name/value JavaScript is case sensitive Always use strict mode, declared with “use strict” Restrictions
Cannot use a variable without declaring it Cannot write to a read-only property Cannot add properties to non-extensible objects Cannot illegally delete functions and variables Cannot define a property more than once in an object literal Cannot use a parameter name more than once in a function Cannot use reserved words, eval, or arguments, as names for functions and
variables The value of this in a function is no longer the window object Cannot declare functions inside of statements Cannot change the members of the arguments array
6 | SharePoint Saturday Milan – 18 May 20136 | SharePoint Saturday Milan – 18 May 2013
Null and undefined NULL
One of the JavaScript primitive types Represents the absence of value Evaluates to false in Boolean expressions
UNDEFINED Primitive type Represents an unknown value Returned when a non-existent object property is called Evaluates to false in Boolean expressions
7 | SharePoint Saturday Milan – 18 May 20137 | SharePoint Saturday Milan – 18 May 2013
Avoid coercive equality operators Objects are only equal to themselves Primitives are equal if the values match (“salvo” === “salvo”) Two sets of equality operators ( == and ===) Never use == or != INSTEAD of === or !==
0 == '0'; // true0 === '0'; // falsefalse == '0'; // truefalse === '0'; // false
8 | SharePoint Saturday Milan – 18 May 20138 | SharePoint Saturday Milan – 18 May 2013
Prototype
JavaScript does have inheritance and usually make use of prototype Every JavaScript object has an internal property called prototype obj.hasOwnProperty('propName') To avoid the duplication of effort and save memory, u can add common
properties and methods to the prototype property. It’s best if you don’t augment built-in prototypes. You can make an
exception when:1.it’s expected that future EMACScript versions while waiting for the
browser to catch up2.You check if your custom property or method doesn’t exist already3.You clearly document and communicate the change with the team
9 | SharePoint Saturday Milan – 18 May 2013
Demo
Null, undefined, equality, prototype
10 | SharePoint Saturday Milan – 18 May 201310 | SharePoint Saturday Milan – 18 May 2013
Functions A function start with the keyword function A function can have a name or not A function can have parameters The delimiters of the function are { } A function can return a value, and that value can be itself Cannot be overloaded!!! Parameters not passed are setted undefined Function without parameters has a default param called (arguments) It’s possible to have a function inside a function Closure Functions have this
11 | SharePoint Saturday Milan – 18 May 201311 | SharePoint Saturday Milan – 18 May 2013
Closure Pattern Whenever you see the function keyword within another function, the
inner function has access to variables of the outer function. Why use it:
it is the local variables for a function — kept alive after the function has returned
it is a stack-frame which is not deallocated when the function returns
12 | SharePoint Saturday Milan – 18 May 201312 | SharePoint Saturday Milan – 18 May 2013
Classes Pattern We create a class in JavaScript by a pattern Use it to prevent name collisions and polluting parent namespace Keep everything tidy Use the new keyword to invoke the constructor
13 | SharePoint Saturday Milan – 18 May 201313 | SharePoint Saturday Milan – 18 May 2013
Module Pattern The window object in browsers is a global namespace Variables defined outside a function are in the global namespace Variables defined without the var keyword are in the global namespace Always create your own namespace by a pattern The module pattern was made by Eric Miraglia of YUI in the 2007 Use it to prevent name collisions and polluting parent namespace It provides structure and helps organize your code as it grows It’s a combination of the following patterns:
Namespaces Immediate functions Private and privileged members Declaring dependencies
14 | SharePoint Saturday Milan – 18 May 201314 | SharePoint Saturday Milan – 18 May 2013
Decorator Pattern It solves the problem of adding or changing functionality on a class
without creating a subclass for every combination of functionality Every time you add a new option, you create only one more class, rather
than doubling the number of classes. The decorator works by wrapping the base object with a decorator object
that has the same interface as the base object
15 | SharePoint Saturday Milan – 18 May 2013
Demo
Closure, Class, Module, Chain, Decorator
16 | SharePoint Saturday Milan – 18 May 2013
Q&A
17 | SharePoint Saturday Milan – 18 May 201317 | SharePoint Saturday Milan – 18 May 2013
Some references Problems about globals:
http://blah.winsmarts.com/2013-5-SharePoint_2013_-_JavaScript_-and-amp;_jQuery_big_booboo_to_watch_out_for.aspx
JavaScript Patterns: http://www.amazon.co.uk/dp/0596806752/?tag=hydra0b-21&hvadid=9550950789&ref=asc_df_0596806752
18 | SharePoint Saturday Milan – 18 May 201318 | SharePoint Saturday Milan – 18 May 2013
Session Feedback
JavaScript Patternshttp://www.surveymonkey.com/s/PNC3CLW
19 | SharePoint Saturday Milan – 18 May 2013
Grazie