• Non ci sono risultati.

JavaScript Object Model

N/A
N/A
Protected

Academic year: 2021

Condividi "JavaScript Object Model"

Copied!
4
0
0

Testo completo

(1)

JavaScript Object Model

Giuseppe Attardi

Università di Pisa

(2)

JavaScript in two slides

Objects map strings to values (properties):

var obj = new Object;

obj["prop"] = 42; => obj.prop obj["0"] = “hello”; => obj[0]

Functions are first-class objects:

function fact(n) {

return (n == 0) ? 1 : n * fact(n-1);

}

fact.desc = "Factorial function";

(3)

JS in two slides (2)

So methods are function-valued properties:

obj.frob = function (n) { this.prop += n;

};

obj.frob(6); => obj.prop == 48

Permissiveness throughout. Oops.

grob = obj.frob; => var not necessary

grob(6); => undefined + 6 == NaN prop = “hello”; => reset global prop

grob(6); => prop == “hello6”

(4)

Full Lexical Closures

Y = lf. (lx. x x) (lx. f(lv. (x x) v) Y f v = f(Y f) v

function Y(f) {

return function (x) { return x(x); }(

function (x) {return f(function (v) { return x(x)(v);

});

});

}

var fact = Y(function (fact) { return function (n) {

return (n == 0) ? 1 : n * fact(n-1);

} });

fact(5); => 120

Riferimenti

Documenti correlati

- The form just has two password input boxes to get the passwords and Reset and Submit buttons - The event handler is triggered by the

The DOM also specifies a NodeList [p.43] interface to handle ordered lists of Nodes [p.34] , such as the children of a Node [p.34] , or the elements [p.98] returned by

This specification defines the Document Object Model Level 2 Events, a platform- and language-neutral interface that gives to programs and scripts a generic event system.. The

This specification defines the Document Object Model Level 2 HTML, a platform- and language-neutral interface that allows programs and scripts to dynamically access and update

This method is used to get the Rect value. If this CSS value doesn’t contain a rect value, a DOMException is raised. Modification to the corresponding style property can be

The default view can be used in order to identify and/or associate a view with its target document (by testing object equality on the AbstractView [p.10] or obtaining the

A NameList , as described in [DOM Level 3 Core], of all possible attribute information items or wildcards [p.14] that can appear as attributes of this element, or null if this

funzionerebbe anche se age non esistesse prima (verrebbe aggiunto al volo, solo in