ECMAScript 5: The New Parts Level 7. Complete implementations of ECMAScript, Fifth Edition, are now in the best web browsers. Including IE10.

Slides:



Advertisements
Similar presentations
The JavaScript Programming Language
Advertisements

Secure ECMAScript Name Subject To Change
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
the javascript language BY SA.
WEAVING CODE EXTENSIONS INTO JAVASCRIPT Benjamin Lerner, Herman Venter, and Dan Grossman University of Washington, Microsoft Research.
Today Programming Style and Your Brain And Then There Was JavaScript Function the Ultimate The Metamorphosis of Ajax ES5: The New Parts.
The Web Warrior Guide to Web Design Technologies
I just met you, and 'this' is crazy, but here's my NaN, so call(me) maybe? JavaScript is so weird.
Julian on JavaScript: Objects Julian M Bucknall, CTO.
JSON, ADsafe, and Misty Douglas Crockford Yahoo!.
Doug Holland Sr. Architect Evangelist Microsoft Corporation Html 5 & EcmaScript 5 Windows Internet Explorer 9 & 10.
12-Jun-15 JavaScript Language Fundamentals I. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
Taking JavaScript Seriously IS NOT THE WORST IDEA.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Javascript & DOM. Javascript – main properties is a lightweight scripting language (language used to control the browser) that is run on the client-side.
Week 9 PHP Cookies and Session Introduction to JavaScript.
JavaScript: The Language Andrew Miadowicz Program Manager DEV307.
Chapter 8 Cookies And Security JavaScript, Third Edition.
Introduction to JavaScript Gordon Tian
Using Client-Side Scripts to Enhance Web Applications 1.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
4.4 JavaScript (JS) Deitel Ch. 7, 8, 9, JavaScript & Java: Similarities JS (JavaScript) is case-sensitive Operators –arithmetic: unary +, unary.
Changes to JavaScript, Part 1: EcmaScript 5 Mark S. Miller, Waldemar Horwat, Mike Samuel Your EcmaScript committee representatives.
Javascript. What is JavaScript? Scripting (interpreted) language designed for the web Beware: JavaScript is case sensitive.
ECMAScript 3.1 Object Model Allen Wirfs-Brock Microsoft.
Prototypal Inheritance. Can We Do Inheritance Without Classes? How do we share behaviour across objects.
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Function the Ultimate Act III. Function Method Class Constructor Module.
LING 408/508: Programming for Linguists Lecture 14 October 19 th.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
MASTERING NAMESPACES! Илья Кантор EcmaScript 5.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 JavaScript 2.0: Evolving a Language for Evolving Systems Waldemar Horwat Netscape.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
JavaScript Introduction
CS5220 Advanced Topics in Web Programming JavaScript and jQuery
Variables and Arithmetic Operators in JavaScript
Modern JavaScript Develop And Design
SEEM4570 Tutorial 05: JavaScript as OOP
Scope, Objects, Strings, Numbers
Modern JavaScript Luke Hoban Program Manager — JavaScript
Juriy Bura
JavaScript an introduction.
Web Systems Development (CSC-215)
Defining Your Own Classes Part 1
WEB PROGRAMMING JavaScript.
JavaScript: The Language
PHP.
CS5220 Advanced Topics in Web Programming JavaScript Basics
Tutorial 4 JavaScript as OOP Li Xu
University of Kurdistan
JavaScript Reserved Words
CS5220 Advanced Topics in Web Programming Node.js Basics
CS3220 Web and Internet Programming JavaScript Basics
JavaScript CS 4640 Programming Languages for Web Applications
Javascript & DOM.
CS3220 Web and Internet Programming JavaScript Basics
JavaScript Session III
Chengyu Sun California State University, Los Angeles
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

ECMAScript 5: The New Parts Level 7

Complete implementations of ECMAScript, Fifth Edition, are now in the best web browsers. Including IE10.

ECMAScript 1999 Third EditionES Fifth EditionES5 –Default –Strict For the short term, work in the intersection of ES3 and ES5/Strict. For the long term, work with ES5/Strict. Avoid ES5/Default.

A better JavaScript.

Lots of Little Things Make the standard conform better to reality. Make the browsers conform better to each other. Where the browsers disagreed, we took license to correct the standard. Interoperability will be improved. If you program well, this should have little impact on you.

Goals of ES5 Don't break the web. Improve the language for the users of the language. Third party security (mashups). Protect stupid people from themselves. No new syntax.

New Syntax Causes syntax errors on IE < 9.

Trailing Commas { "trailing": "comma", } [ "trailing", "comma", ]

Reserved Word Relaxation No reserved word restrictions on property names. var a = { return: true, function: liberty, class: 'classitis', float: 'left' }; a.return = false; a.function(); alert(a.class);

Getters and Setters function make_temperature(temperature) { return { get celsius() { return temperature; }, set celsius(value) { temperature = value; }, get fahrenheit() { return temperature * 9 / ; }, set fahrenheit(value) { temperature = (value - 32) * 5 / 9; } }; }

Multiline string literals var long_line_1 = "This is a \ long line"; // ok var long_line_2 = "This is a \ long line"; // syntax error

Constants Infinity NaN undefined

parseInt parseInt('08') === 8

Regexp Literals /regexp/ now produces new regular expression objects every time.

Replacing Object or Array does not change the behavior of {} or []. As if by the original

JSON JSON.parse(text, reviver) JSON.stringify( value, replacer, space ) json2.js

Brand New Methods Methods can be added without breaking syntax.

Function.prototype.bind if (!Function.prototype.hasOwnProperty('bind')) { Function.prototype.bind = function (object) { var slice = Array.prototype.slice, func = this, args = slice.call(arguments, 1); return function () { return func.apply(object, args.concat( slice.call(arguments, 0))); }; }

String.prototype.trim if (!String.prototype.hasOwnProperty('trim')) { String.prototype.trim = (function (re) { return function () { return this.replace(re, "$1"); }; }(/^\s*(\S*(\s+\S+)*)\s*$/)); }

Array.prototype.every if (!Array.prototype.hasOwnProperty('every')) { Array.prototype.every = function (fun, thisp) { var i, length = this.length; for (i = 0; i < length; i += 1) { if (this.hasOwnProperty(i) && !fun.call(thisp, this[i], i, this)) { return false; } return true; }; }

Array.prototype.filter if (!Array.prototype.hasOwnProperty('filter')) { Array.prototype.filter = function (fun, thisp) { var i, length = this.length, result = [], value; for (i = 0; i < length; i += 1) { if (this.hasOwnProperty(i)) { value = this[i]; if (fun.call(thisp, value, i, this)) { result.push(value); } return result; }; }

Array.prototype.forEach if (!Array.prototype.hasOwnProperty('forEach')) { Array.prototype.forEach = function (fun, thisp) { var i, length = this.length; for (i = 0; i < length; i += 1) { if (this.hasOwnProperty(i)) { fun.call(thisp, this[i], i, this); } }; }

Array.prototype.indexOf if (!Array.prototype.hasOwnProperty('indexOf')) { Array.prototype.indexOf = function (searchElement, fromIndex) { var i = fromIndex || 0, length = this.length; while (i < length) { if (this.hasOwnProperty(i) && this[i] === searchElement) { return i; } i += 1; } return -1; }; }

Array.prototype.lastIndexOf if (!Array.prototype.hasOwnProperty('lastIndexOf')) { Array.prototype.lastIndexOf = function (searchElement, fromIndex) { var i = fromIndex; if (typeof i !== 'number') { i = this.length - 1; } while (i >= 0) { if (this.hasOwnProperty(i) && this[i] === searchElement) { return i; } i -= 1; } return -1; }; }

Array.prototype.map if (!Array.prototype.hasOwnProperty('map')) { Array.prototype.map = function (fun, thisp) { var i, length = this.length, result = []; for (i = 0; i < length; i += 1) { if (this.hasOwnProperty(i)) { result[i] = fun.call(thisp, this[i], i, this); } return result; }; }

Array.prototype.reduce if (!Array.prototype.hasOwnProperty('reduce')) { Array.prototype.reduce = function (fun, initialValue) { var i, length = this.length; for (i = 0; i < length; i += 1) { if (this.hasOwnProperty(i)) { initialValue = fun.call(undefined, initialValue, this[i], i, this); } return initialValue; }; }

Array.prototype.reduceRight if (!Array.prototype.hasOwnProperty('reduceRight')) { Array.prototype.reduceRight = function (fun, initialValue) { var i = this.length - 1; while (i >= 0) { if (this.hasOwnProperty(i)) { initialValue = fun.call(undefined, initialValue, this[i], I, this); } i -= 1; } return initialValue; }; }

Array.prototype.some if (!Array.prototype.hasOwnProperty('some')) { Array.prototype.some = function (fun, thisp) { var i, length = this.length; for (i = 0; i < length; i += 1) { if (this.hasOwnProperty(i) && fun.call(thisp, this[i], i, this)) { return true; } return false; }; }

Date.now() if (!Date.hasOwnProperty('now')) { Date.now = function () { return (new Date()).getTime(); }; }

Date.prototype.toISOString if (!Date.prototype.hasOwnProperty('toISOString')) { Date.prototype.toISOString = function () { function f(n) { return n < 10 ? '0' + n : n; } return this.getUTCFullYear() + '-' + f(this.getUTCMonth() + 1) + '-' + f(this.getUTCDate()) + 'T' + f(this.getUTCHours()) + ':' + f(this.getUTCMinutes()) + ':' + f(this.getUTCSeconds()) + 'Z'; }; }

Date new Date( string ) and Date.parse( string ) will try ISO format first.

Array.isArray if (!Array.hasOwnProperty('isArray')) { Array.isArray = function (value) { return Object.prototype.toString.apply(value) === '[object Array]'; }; }

Object.keys if (!Object.hasOwnProperty('keys')) { Object.keys = function (object) { var name, result = []; for (name in object) { if (Object.prototype.hasOwnProperty.call(object, name)) { result.push(name); } return result; }; }

Object.create if (!Object.hasOwnProperty('create')) { Object.create = function (object, properties) { var result; function F() {} F.prototype = object; result = new F(); if (properties !== undefined) { Object.defineOwnProperties(object, properties); } return result; }; }

Meta Object API Control over the attributes of the properties of the objects.

Two kinds of properties: Data properties Accessor properties

Attributes A property is a named collection of attributes. value:any JavaScript value writeable:boolean enumerable:boolean configurable:boolean get:function () {… return value;} set:function (value) { … }

Data Property var my_object = {foo: bar}; var my_object = Object.create(Object.prototype); Object.defineProperty(my_object, 'foo', { value: bar, writeable: true, enumerable: true, configurable: true });

Accessor property Object.defineProperty(my_object, 'inch', { get: function () { return this.mm / 25.4; }, set: function (value) { this.mm = value * 25.4; }, enumerable: true });

Meta Object API Object.defineProperty( object, key, descriptor ) Object.defineProperties( object, object_of_descriptors ) Object.getOwnPropertyDescriptor( object, key )

Object.getOwnPropertyNames( object ) Object.getPrototypeOf( object ) Best not to use these. Secure frameworks may ban their use.

function replace_prototype(object, prototype) { var result = Object.create(prototype); Object.getOwnPropertyNames(object).forEach(function (key) { Object.defineProperty(result, key, Object.getOwnPropertyDescriptor( object, key)); }); return result; }

Object Extensibility Object.preventExtentions( object ) Object.seal( object ) Object.freeze( object ) Object.isExtensible( object ) Object.isSealed( object ) Object.isFrozen( object )

Strict Mode The most important new feature in ECMAScript, Fifth Edition.

Strict Mode Backward compatible pragma. 'use strict'; File form. First statement in a file. (avoid because of Steve Souders) Function form. First statement in a function.

New Reserved Words implements interface let package private protected public static yield

Strict Mode No more implied global variables within functions. this is not bound to the global object by function form. apply and call do not default to the global object. No with statement. Setting a writeable: false property will throw. Deleting a configurable: false property will throw. Restrictions on eval. eval and arguments are reserved. arguments not linked to parameters. No more arguments.caller or arguments.callee. No more octal literals. Duplicate names in an object literal or function parameters are a syntax error.

Forgetting to use the new prefix in strict mode will now throw an exception, not silently clobber the global object. Death Before Confusion!

Strict Mode addEventListener(…); //error window.addEventListener(…); //ok

Strict Mode There are no methods for determining if strict mode is on, but it is easy to make your own. function in_strict_mode() { return (function () { return !this; }()); } function strict_mode_implemented() { return (function () { 'use strict'; return !this; }()); }

MASH UPS

Safe JavaScript Subsets The design of Strict Mode was informed by safe subsets like Caja & ADsafe. Caja. ADsafe.