JavaScript: A Language of Many Contrasts

Slides:



Advertisements
Similar presentations
The JavaScript Programming Language
Advertisements

Secure ECMAScript Name Subject To Change
The Misconceived Web The original vision of the WWW was as a hyperlinked document-retrieval system. It did.
Object Oriented Programming with Java
the javascript language BY SA.
Code Correctness, Readability, Maintainability Svetlin Nakov Telerik Corporation
Javascript Code Quality Check Tools Javascript Code Quality Check Tools JavaScript was originally intended to do small tasks in webpages, but now JavaScript.
Intro to JavaScript. JavaScript History Client (generally browser-side) language invented at Netscape under the name LiveScript around 1995 Netscape wanted.
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Today Programming Style and Your Brain And Then There Was JavaScript Function the Ultimate The Metamorphosis of Ajax ES5: The New Parts.
Advanced JavaScript Douglas Crockford © 2006 Douglas Crockford.
Julian on JavaScript: Objects Julian M Bucknall, CTO.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
JSON, ADsafe, and Misty Douglas Crockford Yahoo!.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
16-Jun-15 javadoc. 2 Javadoc placement javadoc comments begin with /** and end with */ In a javadoc comment, a * at the beginning of the line is not part.
Advanced Object-Oriented Programming Features
Chapter 1 Principles of Programming and Software Engineering.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Review CSC 171 FALL 2004 LECTURE 21. Topics Objects and Classes Fundamental Types Graphics and Applets Decisions Iteration Designing Classes Testing and.
JAVASCRIPT Introduction Kenny Lam. What is Javascript?  Client-side scripting language that can manipulate elements in the DOM  Event-driven language.
CSE 190M Extra Session #5 JavaScript scope and closures slides created by Marty Stepp
Taking JavaScript Seriously IS NOT THE WORST IDEA.
IS JAVASCRIPT OBJECT-ORIENTED? Contains objects Data Methods Doesn’t have classes Does have constructors Does have prototype functions Doesn’t provide.
JavaScript & Metaperformance Douglas Crockford Yahoo! Inc.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
1 Perl Perl basics Perl Elements Arrays and Hashes Control statements Operators OOP in Perl.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
CMSC 202 Inheritance II. Version 10/102 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Advanced topic - variable.
Object Oriented JavaScript. JavaScript Really only 4 types in JavaScript – number, string, boolean – Object (includes arrays) Remember that an object.
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
1 SystemVerilog Enhancement Requests Daniel Schostak Principal Engineer February 26 th 2010.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Martin Kruliš by Martin Kruliš (v1.0)1.
COMP3190: Principle of Programming Languages
Inter-Type Declarations in AspectJ Awais Rashid Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
Function the Ultimate Act III. Function Method Class Constructor Module.
Julian on JavaScript: Functions Julian M Bucknall, CTO.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
JavaScript: The First Parts Part One Douglas Crockford Yahoo! Inc.
JavaScript scope and closures
“The world’s most misunderstood language has become the world’s most popular programming language” Akshay Arora
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
JavaScript for C# developers Dhananjay Microsoft MVP
Object Oriented JavaScript. JavaScript Really only 4 types in JavaScript – number, string, boolean – Object (includes arrays) Remember that an object.
Getting from Scripts to Applications Donald J. Sipe | Refresh Jacksonville | November 11 th 2008.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
JavaScript Syntax Fort Collins, CO Copyright © XTR Systems, LLC Introduction to JavaScript Syntax Instructor: Joseph DiVerdi, Ph.D., MBA.
Click to add Text Javascript Presented by Bunlong VAN Basic.
JavaScript Programming. What Is JavaScript “the most popular programming language in the world”— w3schools.com “the language for HTML, the Web, computers,
JavaScript Patterns to Clean Up Your Code Dan Wahlin.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Andy Wang Object Oriented Programming in C++ COP 3330
Principles of Programming and Software Engineering
Dr. Charles W. Kann III JavaScript Objects Dr. Charles W. Kann III
Web Systems Development (CSC-215)
More Object-Oriented Programming
CS5220 Advanced Topics in Web Programming JavaScript Basics
CSE 341 Lecture 27 JavaScript scope and closures
Review for Midterm 3.
SPL – PS3 C++ Classes.
Presentation transcript:

JavaScript: A Language of Many Contrasts Douglas Crockford Yahoo! http://javascript.crockford.com/ajaxworld.ppt

The World's Most Misunderstood Programming Language

Sources of Misunderstanding The Name Mispositioning Design Errors Bad Implementations The Browser Bad Books Substandard Standard JavaScript is a Functional Language

Key Ideas Load and go delivery Loose typing Objects as general containers Prototypal inheritance Lambda Linkage through global variables

Key Ideas Load and go delivery Loose typing Objects as general containers Prototypal inheritance Lambda Linkage though global variables

It is full of warts.

For statement Iterate through all of the members of an object: for (var name in object) { if (object.hasOwnProperty(name)) { // within the loop, // name is the key of current member // object[name] is the current value }

typeof The typeof prefix operator returns a string identifying the type of a value. type typeof object 'object' function 'function' array number 'number' string 'string' boolean 'boolean' null undefined 'undefined'

with statement Intended as a short-hand Ambiguous Error-prone Don't use it with (o) { foo = null; } o.foo = null;

It is mostly good stuff.

Inner functions Functions do not all have to be defined at the top level (or left edge). Functions can be defined inside of other functions.

Scope An inner function has access to the variables and parameters of functions that it is contained within. This is known as Static Scoping or Lexical Scoping.

Closure The scope that an inner function enjoys continues even after the parent functions have returned. This is called closure.

Example function fade(id) { var dom = document.getElementById(id), level = 1; function step () { var h = level.toString(16); dom.style.backgroundColor = '#FFFF' + h + h; if (level < 15) { level += 1; setTimeout(step, 100); }

Inheritance Inheritance is object-oriented code reuse. Two Schools: Classical Prototypal

Classical Inheritance Objects are instances of Classes. A Class inherits from another Class.

Pseudoclassical Pseudoclassical looks sort of classical, but is really prototypal. Three mechanisms: Constructor functions. The prototype member of functions. The new operator.

Psuedoclassical function Constructor() { this.member = initializer; return this; // optional } Constructor.prototype.firstMethod = function (a, b) {...}; Constructor.prototype.secondMethod = function (c) {...}; var newObject = new Constructor();

new operator var newObject = new Constructor(); new Constructor() returns a new object with a link to Constructor.prototype. newObject Constructor.prototype

Warning The new operator is required when calling a Constructor. If new is omitted, the global object is clobbered by the constructor, and then the global object is returned instead of a new instance.

----------------------------------- Syntactic Rat Poison Constructor. method('first_method', function (a, b) {...}). method('second_method', function (c) {...}); ----------------------------------- Function.prototype.method = function (name, func) { this.prototype[name] = func; return this; };

Pseudoclassical Inheritance Classical inheritance can be simulated by assigning an object created by one constructor to the prototype member of another. function BiggerConstructor() {...}; BiggerConstructor.prototype = new Constructor(); This does not work exactly like the classical model.

Example function Gizmo(id) { this.id = id; } Gizmo.prototype.toString = function () { return "gizmo " + this.id; };

Example new Gizmo(string) Gizmo Object id string constructor toString function Gizmo(id) { this.id = id; } Gizmo.prototype.toString = function () { return "gizmo " + this.id; }; id string Gizmo constructor toString function prototype Object constructor toString function prototype

Example new Gizmo(string) Gizmo Object id string constructor toString function Gizmo(id) { this.id = id; } Gizmo.prototype.toString = function () { return "gizmo " + this.id; }; id string Gizmo constructor toString function prototype Object constructor toString function prototype

Example new Gizmo(string) Gizmo Object id string constructor toString function Gizmo(id) { this.id = id; } Gizmo.prototype.toString = function () { return "gizmo " + this.id; }; id string Gizmo constructor toString function prototype Object constructor toString function prototype

Inheritance If we replace the original prototype object with an instance of an object of another class, then we can inherit another class's stuff.

Example function Hoozit(id) { this.id = id; } Hoozit.prototype = new Gizmo(); Hoozit.prototype.test = function (id) { return this.id === id; };

Example new Hoozit(string) Gizmo Hoozit id string constructor toString function Hoozit(id) { this.id = id; } Hoozit.prototype = new Gizmo(); Hoozit.prototype.test = function (id) { return this.id === id; }; new Hoozit(string) id string Gizmo constructor toString function prototype Hoozit constructor prototype test function

Example new Hoozit(string) Gizmo Hoozit id string constructor toString function Hoozit(id) { this.id = id; } Hoozit.prototype = new Gizmo(); Hoozit.prototype.test = function (id) { return this.id === id; }; new Hoozit(string) id string Gizmo constructor toString function prototype Hoozit constructor prototype test function

Prototypal Inheritance Class-free. Objects inherit from objects. An object contains a secret link to the object it inherits from. var newObject = object(oldObject); newObject oldObject __proto__

object function A prototypal inheritance language should have an operator like the object function, which makes a new object using an existing object as its prototype.

object function function object(o) { function F() {} F.prototype = o; return new F(); }

object function function object(o) { function F() {} F.prototype = o; return new F(); } newObject = object(oldObject) F prototype constructor

object function function object(o) { function F() {} F.prototype = o; return new F(); } newObject = object(oldObject) F prototype constructor oldObject

object function function object(o) { function F() {} F.prototype = o; return new F(); } newObject = object(oldObject) F prototype newObject oldObject

object function function object(o) { function F() {} F.prototype = o; return new F(); } newObject = object(oldObject) newObject oldObject

Prototypal Inheritance var oldObject = { firstMethod: function () {...}, secondMethod: function () {...} }; var newObject = object(oldObject); newObject.thirdMethod = function () {...}; var myDoppelganger = object(newObject); myDoppelganger.firstMethod();

Prototypal Inheritance There is no limit to the length of the chain (except common sense). myDoppelganger = object(newObject); newObject oldObject

Augmentation Using the object function, we can quickly produce new objects that have the same state and behavior as existing objects. We can then augment each of the instances by assigning new methods and members.

Public Method A Public Method is a function that uses this to access its object. This binding of this to an object happens at invocation time. A Public Method can be reused with many "classes".

Public Methods myObject.method = function (string) { return this.member + string; }; We can put this function in any object at it works. Public methods work extremely well with prototypal inheritance and with pseudoclassical inheritance.

Singletons There is no need to produce a class-like constructor for an object that will have exactly one instance. Instead, simply use an object literal.

Singletons var singleton = { firstMethod: function (a, b) { ... }, secondMethod: function (c) { } };

Functions are used as Functions Methods Constructors Classes Modules

Module Variables defined in a module are only visible in the module. Functions have scope. Variables defined in a function only visible in the function. Functions can be used a module containers.

Global variables are evil Functions within an application can clobber each other. Cooperating applications can clobber each other. Use of the global namespace must be minimized.

Singletons The methods of a singleton can enjoy access to shared private data and private methods.

Singletons var singleton = function () { var privateVariable; function privateFunction(x) { ...privateVariable... } return { firstMethod: function (a, b) { }, secondMethod: function (c) { ...privateFunction()... }; }();

Applications are Singletons var AJAX = function () { var privateVariable; function privateFunction(x) { ...privateVariable... } return { firstMethod: function (a, b) { }, secondMethod: function (c) { ...privateFunction()... }; }();

Privacy All members of an object are public. We want private variables and private methods. Really.

Privileged Method A Privileged Method is a function that has access to secret information. A Privileged Method has access to private variables and private methods. A Privileged Method obtains its secret information through closure.

Power Constructor Put the singleton module pattern in constructor function, and we have a power constructor pattern. Make a new object somehow. Augment it. Return it.

Power Constructor function powerConstructor() { var that = object(oldObject), privateVariable; function privateFunction(x) { ... } that.firstMethod = function (a, b) { ...privateVariable... }; that.secondMethod = function (c) { ...privateFunction()... return that; }

Power Constructor Public methods (from the prototype) var that = object(oldObject); Private variables (var) Private methods (inner functions) Privileged methods No need to use new myObject = power_constructor();

Parasitic Inheritance A power constructor calls another constructor, takes the result, augments it, and returns it as though it did all the work.

Psudeoclassical Inheritance function Gizmo(id) { this.id = id; } Gizmo.prototype.toString = function () { return "gizmo " + this.id; }; function Hoozit(id) { Hoozit.prototype = new Gizmo(); Hoozit.prototype.test = function (id) { return this.id === id;

Parasitic Inheritance function gizmo(id) { return { id: id, toString: function () { return "gizmo " + this.id; } }; function hoozit(id) { var that = gizmo(id); that.test = function (testid) { return testid === this.id; return that;

Secrets function gizmo(id) { return { toString: function () { return "gizmo " + id; } }; function hoozit(id) { var that = gizmo(id); that.test = function (testid) { return testid === id; return that;

Shared Secrets function gizmo(id, secret) { secret = secret || {}; secret.id = id; return { toString: function () { return "gizmo " + secret.id; }; } function hoozit(id) { var secret = {}, that = gizmo(id, secret); that.test = function (testid) { return testid === secret.id; return that;

Super Methods function hoozit(id) { var secret = {}, that = gizmo(id, secret), super_toString = that.toString; that.test = function (testid) { return testid === secret.id; }; that.toString = function () { return super_toString.apply(that, []); return that; }

Inheritance Patterns Prototypal Inheritance works really well with public methods. Parasitic Inheritance works really well with privileged and private and public methods. Pseudoclassical Inheritance for elderly programmers who are old and set in their ways.

Working with the Grain Pseudoclassical patterns are less effective than prototypal patterns or parasitic patterns. Formal classes are not needed for reuse or extension. Be shallow. Deep hierarchies are not effective.

Performance Provide a good experience. Be respectful of our customer's time. Hoare's Dictum: Premature optimization is the root of all evil.

Efficiency The first priority must always be correctness. Optimize when necessary. Consider algorithmic improvements O (n) v O (n log n) v O (n2) Watch for limits.

Minification vs Obfuscation Reduce the amount of source code to reduce download time. Minification deletes whitespace and comments. Obfuscation also changes the names of things. Obfuscation can introduce bugs. Never use tools that cause bugs if you can avoid it. http://www.crockford.com/javascript/jsmin.html

Code Conventions for the JavaScript Programming Language http://javascript.crockford.com/code.html

JSLint JSLint can help improve the robustness and portability of your programs. It enforces style rules. It can spot some errors that are very difficult to find in debugging. It can help eliminate implied globals. Commandline versions. In text editors and Eclipse.

JSLint http://www.JSLint.com/ Warning: JSLint will hurt your feelings. If you follow its advice, JSLint will make your programs better. http://www.JSLint.com/

JavaScript: A Language of Many Contrasts Douglas Crockford Yahoo! http://javascript.crockford.com/ajaxworld.ppt