+ All Categories
Home > Documents > Sharepoint saturday 18 may 2013

Sharepoint saturday 18 may 2013

Date post: 01-Jul-2015
Category:
Upload: salvatore-fazio
View: 216 times
Download: 1 times
Share this document with a friend
19
1 | SharePoint Saturday Milan – 18 May 2013
Transcript
Page 1: Sharepoint saturday   18 may 2013

1 | SharePoint Saturday Milan – 18 May 2013

Page 2: Sharepoint saturday   18 may 2013

2 | SharePoint Saturday Milan – 18 May 20132 | SharePoint Saturday Milan – 18 May 2013

Grazie ai nostri Sponsor!

Page 3: Sharepoint saturday   18 may 2013

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

Page 4: Sharepoint saturday   18 may 2013

4 | SharePoint Saturday Milan – 18 May 20134 | SharePoint Saturday Milan – 18 May 2013

Horizontal session

Page 5: Sharepoint saturday   18 may 2013

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

Page 6: Sharepoint saturday   18 may 2013

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

Page 7: Sharepoint saturday   18 may 2013

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

Page 8: Sharepoint saturday   18 may 2013

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

Page 9: Sharepoint saturday   18 may 2013

9 | SharePoint Saturday Milan – 18 May 2013

Demo

Null, undefined, equality, prototype

Page 10: Sharepoint saturday   18 may 2013

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

Page 11: Sharepoint saturday   18 may 2013

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

Page 12: Sharepoint saturday   18 may 2013

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

Page 13: Sharepoint saturday   18 may 2013

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

Page 14: Sharepoint saturday   18 may 2013

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

Page 15: Sharepoint saturday   18 may 2013

15 | SharePoint Saturday Milan – 18 May 2013

Demo

Closure, Class, Module, Chain, Decorator

Page 16: Sharepoint saturday   18 may 2013

16 | SharePoint Saturday Milan – 18 May 2013

Q&A

Page 17: Sharepoint saturday   18 may 2013

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

Page 18: Sharepoint saturday   18 may 2013

18 | SharePoint Saturday Milan – 18 May 201318 | SharePoint Saturday Milan – 18 May 2013

Session Feedback

JavaScript Patternshttp://www.surveymonkey.com/s/PNC3CLW

Page 19: Sharepoint saturday   18 may 2013

19 | SharePoint Saturday Milan – 18 May 2013

Grazie


Recommended