programming4us
programming4us
WEBSITE

Sharepoint 2013 : Introducing JavaScript for SharePoint developers (part 1) - Understanding JavaScript namespaces, Understanding JavaScript variables

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
7/18/2014 4:33:50 AM

JavaScript takes on increased importance in app development. Therefore, this section presents a brief overview of the JavaScript language and its key characteristics from the perspective of an experienced SharePoint programmer. Although you have probably written some JavaScript, you might not have spent time to understand JavaScript at the level necessary to be successful writing SharePoint apps. If you are a JavaScript expert, you can certainly skip this section.

Understanding JavaScript namespaces

As a SharePoint developer, you have probably written at least some JavaScript in a webpage; thus, you understand that JavaScript code is comprised of functions. These functions can be written directly into the webpage by using script tags or referenced in separate library files. If you are more of a casual JavaScript developer, however, you might not be aware that simply writing a named function places that function in the global namespace. The global namespace is the container into which all variables and functions are placed by default. In the browser, this container is the window object. Cluttering the global namespace with functions can easily lead to naming conflicts and “spaghetti” code.

In addition to the global namespace, you can define your own custom namespaces. Namespaces in JavaScript are essentially just containing objects defined within the global namespace. By using custom namespaces, you can isolate your code from other JavaScript in the page. This is essential for preventing naming conflicts. Custom namespaces are one of the few things that should be defined within the global namespace. Most variables and functions are generally defined within a custom namespace. The following code shows how to define a custom namespace:

var Wingtip = window.Wingtip || {};

The sample code sets a global variable named Wingtip to reference either an existing global variable or creates a new one if it does not exist already. This is the standard approach to creating namespaces because this line of code can exist in several different libraries without causing a naming conflict. The first library loaded with this code present establishes the namespace definition for those loaded later.

Understanding JavaScript variables

Variables in JavaScript can be declared either in a namespace or within a function. Unlike C#, JavaScript variables are not declared by using a data type keyword. Instead, JavaScript uses the var keyword to define a variable. Although not strictly required, variables should always be declared by using the var keyword. This is important because when it is not used, the variable is automatically defined within the global namespace. When the var keyword is used outside of a function, the associated variable is always defined within the global namespace. When it is used within a function, the associated variable is scoped to the function only. The following code shows an example of a global variable, global function, and local variable:

<script type="text/JavaScript">
var myGlobalVar = "This is a global variable";
function myGlobalFunction() {
alert("This function is defined in the global namespace");
for (var i=0; i<5; i++) {
alert("This variable is local to the function: " + i);
}
}
</script>

Variables can be defined within a custom namespace by simply referencing the namespace when using the variable. The code that follows shows how to create a variable within a custom namespace.

var Wingtip = window.Wingtip || {};
var window.Wingtip.myNamespaceVar = "This is a variable defined within a namespace";

Although JavaScript does not have specific data type keywords, declared variables do have a type based on the value they hold. Variable types can be examined by using the typeof operator. The typeof operator returns one of the following values when applied to a variable or function parameter:

  • undefined

  • string

  • number

  • Boolean

  • function

  • object

Because JavaScript is very loose with rules concerning variable and object definitions, you should be sure to always use strict JavaScript in your apps. Strict JavaScript is an improved version of JavaScript. You can enable it by adding the line “use strict” at the top of any library or function. Strict JavaScript will prevent you from making many common mistakes in your code. The following lists some of the key restrictions enabled by strict JavaScript:

  • 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

Other  
  •  Sharepoint 2013 : List and library essentials - Creating and deleting lists
  •  Sharepoint 2013 : List and library essentials - Using your apps
  •  Sharepoint 2013 : Developing Applications Using Office Services - What’s New in Access Services
  •  Sharepoint 2013 : Developing Applications Using Office Services - The New Machine Translation Services
  •  Sharepoint 2013 : Developing Applications Using Office Services - Word Automation Services and the New PowerPoint Automation Services
  •  Sharepoint 2013 : Developing Applications Using Office Services - What’s New in Excel Services
  •  Sharepoint 2013 : Developing Applications Using Office Services - WOPI and the New Office Web Apps Server
  •  Sharepoint 2013 : Building a BCS-enabled Business Solution : Building an Integrated BCS Solution with an App for SharePoint Containing an App for Office
  •  Business Connectivity Services in Apps for SharePoint 2013 : Building an App-level BCS Solution for Office 365 SharePoint Online
  •  Business Connectivity Services in SharePoint 2013 : Adding a Business Data Connectivity Model to Office 365 SharePoint Online
  •  
    Top 10
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
    - Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
    - Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    programming4us programming4us
    programming4us
     
     
    programming4us