SlideShare une entreprise Scribd logo
1  sur  70
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

'object'

number

'number'

string

'string'

boolean

'boolean'

null

'object'

undefined

'undefined'
with statement
• Intended as a
short-hand
• Ambiguous
• Error-prone
• Don't use it

with (o) {
foo = null;
}

 o.foo = null;
 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);
}
}
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;
};
function Gizmo(id) {

this.id = id;
}

Example

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

Gizmo
prototype

Object
prototype

constructor
toString

function

constructor
toString

function

new Gizmo(string)
id

string
function Gizmo(id) {

this.id = id;
}

Example

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

Gizmo
prototype

Object
prototype

constructor
toString

function

constructor
toString

function

new Gizmo(string)
id

string
function Gizmo(id) {

this.id = id;
}

Example

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

Gizmo
prototype

Object
prototype

constructor
toString

function

constructor
toString

function

new Gizmo(string)
id

string
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

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

Gizmo
prototype

Hoozit

id

constructor
toString

function

constructor

prototype

test

new Hoozit(string)

function

string
Example

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

Gizmo
prototype

Hoozit

id

constructor
toString

function

constructor

prototype

test

new Hoozit(string)

function

string
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
__proto__

oldObject
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) {
...privateVariable...
},
secondMethod: function (c) {
...privateFunction()...
}
};
}();
Applications are Singletons
var AJAX = function () {
var privateVariable;
function privateFunction(x) {
...privateVariable...
}
return {
firstMethod: function (a, b) {
...privateVariable...
},
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.
1. Make a new object somehow.

2. Augment it.
3. 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) {
this.id = 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)

• Watch for limits.

v

O (n2)
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
• 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

Contenu connexe

Tendances

JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
David Padbury
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」
techtalkdwango
 

Tendances (19)

JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
Core concepts-javascript
Core concepts-javascriptCore concepts-javascript
Core concepts-javascript
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
JavaScript Inheritance
JavaScript InheritanceJavaScript Inheritance
JavaScript Inheritance
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 
Advanced javascript
Advanced javascriptAdvanced javascript
Advanced javascript
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
Js: master prototypes
Js: master prototypesJs: master prototypes
Js: master prototypes
 
Lombok Features
Lombok FeaturesLombok Features
Lombok Features
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
 
JavaScript Patterns
JavaScript PatternsJavaScript Patterns
JavaScript Patterns
 
Beginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScriptBeginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScript
 
みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」みゆっき☆Think#7 「本気で学ぶJavascript」
みゆっき☆Think#7 「本気で学ぶJavascript」
 
The many facets of code reuse in JavaScript
The many facets of code reuse in JavaScriptThe many facets of code reuse in JavaScript
The many facets of code reuse in JavaScript
 
Prototype
PrototypePrototype
Prototype
 
Let's JavaScript
Let's JavaScriptLet's JavaScript
Let's JavaScript
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almost
 

En vedette

Інформація по ЗНО 2013 - 2
Інформація по ЗНО 2013 - 2Інформація по ЗНО 2013 - 2
Інформація по ЗНО 2013 - 2
lyceum12369874
 
JUNIOR DESIGN ENGINEER (2)
JUNIOR DESIGN ENGINEER (2)JUNIOR DESIGN ENGINEER (2)
JUNIOR DESIGN ENGINEER (2)
Prem K
 
Orlando.fetchpetcare
Orlando.fetchpetcareOrlando.fetchpetcare
Orlando.fetchpetcare
deannalagason
 
Інформація по ЗНО 2013
Інформація по ЗНО 2013Інформація по ЗНО 2013
Інформація по ЗНО 2013
lyceum12369874
 

En vedette (16)

He evening talk v2 2013
He evening talk v2 2013He evening talk v2 2013
He evening talk v2 2013
 
Інформація по ЗНО 2013 - 2
Інформація по ЗНО 2013 - 2Інформація по ЗНО 2013 - 2
Інформація по ЗНО 2013 - 2
 
Trends of social media 2014
Trends of social media 2014Trends of social media 2014
Trends of social media 2014
 
Presentation2
Presentation2Presentation2
Presentation2
 
00769767
0076976700769767
00769767
 
JUNIOR DESIGN ENGINEER (2)
JUNIOR DESIGN ENGINEER (2)JUNIOR DESIGN ENGINEER (2)
JUNIOR DESIGN ENGINEER (2)
 
Revista scrap
Revista scrapRevista scrap
Revista scrap
 
Social media trends for 2015
Social media trends for 2015Social media trends for 2015
Social media trends for 2015
 
Five Star Premier Residences of Pompano Beach
Five Star Premier Residences of Pompano BeachFive Star Premier Residences of Pompano Beach
Five Star Premier Residences of Pompano Beach
 
преза
презапреза
преза
 
Orlando.fetchpetcare
Orlando.fetchpetcareOrlando.fetchpetcare
Orlando.fetchpetcare
 
Інформація по ЗНО 2013
Інформація по ЗНО 2013Інформація по ЗНО 2013
Інформація по ЗНО 2013
 
Kumar2011
Kumar2011Kumar2011
Kumar2011
 
Unique copy
Unique   copyUnique   copy
Unique copy
 
Intelligent storytelling: How to write good stories for social media
Intelligent storytelling: How to write good stories for social mediaIntelligent storytelling: How to write good stories for social media
Intelligent storytelling: How to write good stories for social media
 
Mengelola pedagang eceran, grosir, dan logistik
Mengelola pedagang eceran, grosir, dan logistikMengelola pedagang eceran, grosir, dan logistik
Mengelola pedagang eceran, grosir, dan logistik
 

Similaire à Ajaxworld

JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course
yoavrubin
 
Java script prototype_hack
Java script prototype_hackJava script prototype_hack
Java script prototype_hack
LearningTech
 

Similaire à Ajaxworld (20)

Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
JS Essence
JS EssenceJS Essence
JS Essence
 
Javascript Common Design Patterns
Javascript Common Design PatternsJavascript Common Design Patterns
Javascript Common Design Patterns
 
JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
C questions
C questionsC questions
C questions
 
Week 06 Modular Javascript_Brandon, S. H. Wu
Week 06 Modular Javascript_Brandon, S. H. WuWeek 06 Modular Javascript_Brandon, S. H. Wu
Week 06 Modular Javascript_Brandon, S. H. Wu
 
JavsScript OOP
JavsScript OOPJavsScript OOP
JavsScript OOP
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Understanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG JuneUnderstanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG June
 
Javascript talk
Javascript talkJavascript talk
Javascript talk
 
About Python
About PythonAbout Python
About Python
 
Javascript under the hood 2
Javascript under the hood 2Javascript under the hood 2
Javascript under the hood 2
 
Javascript Workshop
Javascript WorkshopJavascript Workshop
Javascript Workshop
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java script
 
java script functions, classes
java script functions, classesjava script functions, classes
java script functions, classes
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
The prototype property
The prototype propertyThe prototype property
The prototype property
 
Java script prototype_hack
Java script prototype_hackJava script prototype_hack
Java script prototype_hack
 

Plus de deannalagason

Pittsburgh cranberry-wexford.fetchpetcare
Pittsburgh cranberry-wexford.fetchpetcarePittsburgh cranberry-wexford.fetchpetcare
Pittsburgh cranberry-wexford.fetchpetcare
deannalagason
 
Orlando.fetchpetcare
Orlando.fetchpetcareOrlando.fetchpetcare
Orlando.fetchpetcare
deannalagason
 
Simplified.realpropertymgt1
Simplified.realpropertymgt1Simplified.realpropertymgt1
Simplified.realpropertymgt1
deannalagason
 
Tri state-area.realpropertymgt1
Tri state-area.realpropertymgt1Tri state-area.realpropertymgt1
Tri state-area.realpropertymgt1
deannalagason
 
Hartford metro.realpropertymgt1
Hartford metro.realpropertymgt1Hartford metro.realpropertymgt1
Hartford metro.realpropertymgt1
deannalagason
 

Plus de deannalagason (18)

Pull the chain for apartment in cairns
Pull the chain for apartment in cairnsPull the chain for apartment in cairns
Pull the chain for apartment in cairns
 
Commercial painterts to paint all your dreams
Commercial painterts to paint all your dreamsCommercial painterts to paint all your dreams
Commercial painterts to paint all your dreams
 
Coral Oaks FiveStar Senior Living
Coral Oaks FiveStar Senior LivingCoral Oaks FiveStar Senior Living
Coral Oaks FiveStar Senior Living
 
Marriage & Family Counseling
Marriage & Family CounselingMarriage & Family Counseling
Marriage & Family Counseling
 
Fetch- A Renowned Dog Walker
Fetch- A Renowned Dog WalkerFetch- A Renowned Dog Walker
Fetch- A Renowned Dog Walker
 
Care for Seniors
Care for SeniorsCare for Seniors
Care for Seniors
 
Fetch- Helps in Meeting the Requirement of the Pet
Fetch- Helps in Meeting the Requirement of the PetFetch- Helps in Meeting the Requirement of the Pet
Fetch- Helps in Meeting the Requirement of the Pet
 
Train Your Pet to Make Your Life Easy
Train Your Pet to Make Your Life EasyTrain Your Pet to Make Your Life Easy
Train Your Pet to Make Your Life Easy
 
Pittsburgh cranberry-wexford.fetchpetcare
Pittsburgh cranberry-wexford.fetchpetcarePittsburgh cranberry-wexford.fetchpetcare
Pittsburgh cranberry-wexford.fetchpetcare
 
Orlando.fetchpetcare
Orlando.fetchpetcareOrlando.fetchpetcare
Orlando.fetchpetcare
 
Trusted and quality pet care at all times!
Trusted and quality pet care at all times!Trusted and quality pet care at all times!
Trusted and quality pet care at all times!
 
marriage counseling miami
marriage counseling miamimarriage counseling miami
marriage counseling miami
 
Simplified.realpropertymgt1
Simplified.realpropertymgt1Simplified.realpropertymgt1
Simplified.realpropertymgt1
 
Tri state-area.realpropertymgt1
Tri state-area.realpropertymgt1Tri state-area.realpropertymgt1
Tri state-area.realpropertymgt1
 
Hartford metro.realpropertymgt1
Hartford metro.realpropertymgt1Hartford metro.realpropertymgt1
Hartford metro.realpropertymgt1
 
Expressrpm1
Expressrpm1Expressrpm1
Expressrpm1
 
Lidarus
LidarusLidarus
Lidarus
 
Lidarus
LidarusLidarus
Lidarus
 

Dernier

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Dernier (20)

Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 

Ajaxworld

  • 1. JavaScript: A Language of Many Contrasts Douglas Crockford Yahoo! http://javascript.crockford.com/ajaxworld.ppt
  • 3. Sources of Misunderstanding • • • • • • • • The Name Mispositioning Design Errors Bad Implementations The Browser Bad Books Substandard Standard JavaScript is a Functional Language
  • 4. Key Ideas • Load and go delivery • Loose typing • Objects as general containers • Prototypal inheritance • Lambda • Linkage through global variables
  • 5. Key Ideas • Load and go delivery • Loose typing • Objects as general containers • Prototypal inheritance • Lambda • Linkage though global variables
  • 6. It is full of warts.
  • 7. 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 } }
  • 8. typeof • The typeof prefix operator returns a string identifying the type of a value. type typeof object 'object' function 'function' array 'object' number 'number' string 'string' boolean 'boolean' null 'object' undefined 'undefined'
  • 9. with statement • Intended as a short-hand • Ambiguous • Error-prone • Don't use it with (o) { foo = null; }  o.foo = null;  foo = null;
  • 10. It is mostly good stuff.
  • 11. 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.
  • 12. 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.
  • 13. Closure • The scope that an inner function enjoys continues even after the parent functions have returned. • This is called closure.
  • 14. 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); } } setTimeout(step, 100); }
  • 15. Inheritance • Inheritance is object-oriented code reuse. • Two Schools: • Classical • Prototypal
  • 16. Classical Inheritance • Objects are instances of Classes. • A Class inherits from another Class.
  • 17. Pseudoclassical • Pseudoclassical looks sort of classical, but is really prototypal. • Three mechanisms: • Constructor functions. • The prototype member of functions. • The new operator.
  • 18. Psuedoclassical function Constructor() { this.member = initializer; return this; // optional } Constructor.prototype.firstMethod = function (a, b) {...}; Constructor.prototype.secondMethod = function (c) {...}; var newObject = new Constructor();
  • 19. new operator var newObject = new Constructor(); • new Constructor() returns a new object with a link to Constructor.prototype. newObject Constructor.prototype
  • 20. 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.
  • 21. 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; };
  • 22. 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.
  • 23. Example function Gizmo(id) { this.id = id; } Gizmo.prototype.toString = function () { return "gizmo " + this.id; };
  • 24. function Gizmo(id) { this.id = id; } Example Gizmo.prototype.toString = function () { return "gizmo " + this.id; }; Gizmo prototype Object prototype constructor toString function constructor toString function new Gizmo(string) id string
  • 25. function Gizmo(id) { this.id = id; } Example Gizmo.prototype.toString = function () { return "gizmo " + this.id; }; Gizmo prototype Object prototype constructor toString function constructor toString function new Gizmo(string) id string
  • 26. function Gizmo(id) { this.id = id; } Example Gizmo.prototype.toString = function () { return "gizmo " + this.id; }; Gizmo prototype Object prototype constructor toString function constructor toString function new Gizmo(string) id string
  • 27. 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.
  • 28. Example function Hoozit(id) { this.id = id; } Hoozit.prototype = new Gizmo(); Hoozit.prototype.test = function (id) { return this.id === id; };
  • 29. Example function Hoozit(id) { this.id = id; } Hoozit.prototype = new Gizmo(); Hoozit.prototype.test = function (id) { return this.id === id; }; Gizmo prototype Hoozit id constructor toString function constructor prototype test new Hoozit(string) function string
  • 30. Example function Hoozit(id) { this.id = id; } Hoozit.prototype = new Gizmo(); Hoozit.prototype.test = function (id) { return this.id === id; }; Gizmo prototype Hoozit id constructor toString function constructor prototype test new Hoozit(string) function string
  • 31. 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 __proto__ oldObject
  • 32. 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.
  • 33. object function function object(o) { function F() {} F.prototype = o; return new F(); }
  • 34. object function function object(o) { function F() {} F.prototype = o; return new F(); } newObject = object(oldObject) F prototype constructor
  • 35. object function function object(o) { function F() {} F.prototype = o; return new F(); } newObject = object(oldObject) F prototype constructor oldObject
  • 36. object function function object(o) { function F() {} F.prototype = o; return new F(); } newObject = object(oldObject) F prototype newObject oldObject
  • 37. object function function object(o) { function F() {} F.prototype = o; return new F(); } newObject = object(oldObject) newObject oldObject
  • 38. Prototypal Inheritance var oldObject = { firstMethod: function () {...}, secondMethod: function () {...} }; var newObject = object(oldObject); newObject.thirdMethod = function () {...}; var myDoppelganger = object(newObject); myDoppelganger.firstMethod();
  • 39. Prototypal Inheritance • There is no limit to the length of the chain (except common sense). myDoppelganger = object(newObject); newObject oldObject
  • 40. 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.
  • 41. 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".
  • 42. 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.
  • 43. 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.
  • 44. Singletons var singleton = { firstMethod: function (a, b) { ... }, secondMethod: function (c) { ... } };
  • 45. Functions are used as • Functions • Methods • Constructors • Classes • Modules
  • 46. 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.
  • 47. 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.
  • 48. Singletons • The methods of a singleton can enjoy access to shared private data and private methods.
  • 49. Singletons var singleton = function () { var privateVariable; function privateFunction(x) { ...privateVariable... } return { firstMethod: function (a, b) { ...privateVariable... }, secondMethod: function (c) { ...privateFunction()... } }; }();
  • 50. Applications are Singletons var AJAX = function () { var privateVariable; function privateFunction(x) { ...privateVariable... } return { firstMethod: function (a, b) { ...privateVariable... }, secondMethod: function (c) { ...privateFunction()... } }; }();
  • 51. Privacy • All members of an object are public. • We want private variables and private methods. • Really.
  • 52. 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.
  • 53. Power Constructor • Put the singleton module pattern in constructor function, and we have a power constructor pattern. 1. Make a new object somehow. 2. Augment it. 3. Return it.
  • 54. 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; }
  • 55. 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();
  • 56. Parasitic Inheritance • A power constructor calls another constructor, takes the result, augments it, and returns it as though it did all the work.
  • 57. Psudeoclassical Inheritance function Gizmo(id) { this.id = id; } Gizmo.prototype.toString = function () { return "gizmo " + this.id; }; function Hoozit(id) { this.id = id; } Hoozit.prototype = new Gizmo(); Hoozit.prototype.test = function (id) { return this.id === id; }
  • 58. 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; }
  • 59. 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; }
  • 60. 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; }
  • 61. 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; }
  • 62. 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.
  • 63. 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.
  • 64. Performance • Provide a good experience. • Be respectful of our customer's time. • Hoare's Dictum: Premature optimization is the root of all evil.
  • 65. Efficiency • The first priority must always be correctness. • Optimize when necessary. • Consider algorithmic improvements O (n) v O (n log n) • Watch for limits. v O (n2)
  • 66. 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
  • 67. Code Conventions for the JavaScript Programming Language http://javascript.crockford.com/code.html
  • 68. 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.
  • 69. JSLint • Warning: JSLint will hurt your feelings. • If you follow its advice, JSLint will make your programs better. •http://www.JSLint.com/
  • 70. JavaScript: A Language of Many Contrasts Douglas Crockford Yahoo! http://javascript.crockford.com/ajaxworld.ppt