Essentials of Javascript - Cultural View
Essentials of Javascript - Cultural View Essentials of Javascript - Cultural View
JavaScript syntax 39 Constructors Constructor functions simply assign values to slots of a newly created object. The values may be data or other functions. Example: Manipulating an object function MyObject(attributeA, attributeB) { } this.attributeA = attributeA; this.attributeB = attributeB; MyObject.staticC = "blue"; // On MyObject Function, not obj alert(MyObject.staticC); // blue obj = new MyObject('red', 1000); alert(obj.attributeA); // red alert(obj["attributeB"]); // 1000 alert(obj.staticC); // undefined obj.attributeC = new Date(); // add a new property delete obj.attributeB; // remove a property of obj alert(obj.attributeB); // undefined delete obj; // remove the whole Object (rarely used) alert(obj.attributeA); // throws an exception The constructor itself is stored in the special slot constructor. So x = new Foo() // Above is almost equivalent to y = {}; y.constructor = Foo; y.constructor(); // except x.constructor == y.constructor // true x instanceof Foo // true y instanceof Foo // false, surprisingly. Functions are objects themselves, which can be used to produce an effect similar to "static properties" (using C++/Java terminology) as shown below. (The function object also has a special prototype property, as discussed in the Inheritance section below.) Object deletion is rarely used as the scripting engine will garbage collect objects that are no longer being referenced.
JavaScript syntax 40 Inheritance JavaScript supports inheritance hierarchies through prototyping in the manner of Self. In the following example, the Derive class inherits from the Base class. When d is created as a Derive, the reference to the base instance of Base is copied to d. base. Derive does not contain a value for aBaseFunction, so it is retrieved from Base when aBaseFunction is accessed. This is made clear by changing the value of base.aBaseFunction, which is reflected in the value of d.aBaseFunction. Some implementations allow the prototype to be accessed or set explicitly using the proto slot as shown below. function Base() { } this.anOverride = function() {alert("Base::anOverride()");}; this.aBaseFunction = function() {alert("Base::aBaseFunction()");}; function Derive() { } this.anOverride = function() {alert("Derive::anOverride()");}; base = new Base(); Derive.prototype = base; // Must be before new Derive() d = new Derive(); // Copies Derive.prototype to d instance's hidden prototype slot. base.aBaseFunction = function() {alert("Base::aNEWBaseFunction()")} d.anOverride(); // Derive::anOverride() d.aBaseFunction(); // Base::aNEWBaseFunction() alert(d.aBaseFunction == Derive.prototype.aBaseFunction); // true alert(d. proto == base); // true in Mozilla-based implementations but false in many other implementations The following shows clearly how references to prototypes are copied on instance creation, but that changes to a prototype can affect all instances that refer to it. function m1() {return "One ";} function m2() {return "Two ";} function m3() {return "Three ";} function Base() {} Base.prototype.yyy = m2; bar = new Base(); alert("bar.yyy " + bar.yyy()); // bar.yyy Two function Top(){this.yyy = m3} ttt = new Top();
- Page 1 and 2: Essentials of Javascript Visit the
- Page 3 and 4: John Resig 122 Reverse Ajax 123 Ric
- Page 5 and 6: JavaScript 2 History Anyway I know
- Page 7 and 8: JavaScript 4 Prototype-based protot
- Page 9 and 10: JavaScript 6 LCMCalculator.prototyp
- Page 11 and 12: JavaScript 8 Example - use in web p
- Page 13 and 14: JavaScript 10 request that might ha
- Page 15 and 16: JavaScript 12 Application platform
- Page 17 and 18: JavaScript 14 See also • Client-s
- Page 19 and 20: JavaScript 16 [25] Robert Nyman, Ge
- Page 21 and 22: JavaScript syntax 18 JavaScript syn
- Page 23 and 24: JavaScript syntax 20 Primitive data
- Page 25 and 26: JavaScript syntax 22 String A Strin
- Page 27 and 28: JavaScript syntax 24 Native Objects
- Page 29 and 30: JavaScript syntax 26 } Math The Mat
- Page 31 and 32: JavaScript syntax 28 // ? - 0 or 1
- Page 33 and 34: JavaScript syntax 30 Operators The
- Page 35 and 36: JavaScript syntax 32 alert( Boolean
- Page 37 and 38: JavaScript syntax 34 Conditional op
- Page 39 and 40: JavaScript syntax 36 With The with
- Page 41: JavaScript syntax 38 Methods A meth
- Page 45 and 46: JavaScript syntax 42 See also • C
- Page 47 and 48: JavaScript Style Sheets 44 } fontSi
- Page 49 and 50: JavaScript engine 46 JavaScript eng
- Page 51 and 52: Ajax (programming) 48 Ajax (program
- Page 53 and 54: Ajax (programming) 50 • Ajax-powe
- Page 55 and 56: ?: 52 ?: In computer programming, ?
- Page 57 and 58: ?: 54 Furthermore, the ternary oper
- Page 59 and 60: ?: 56 ?: in style guidelines Some c
- Page 61 and 62: Appcelerator Titanium 58 See also
- Page 63 and 64: Bookmarklet 60 Bookmarklet A bookma
- Page 65 and 66: Bookmarklet 62 References [1] Domai
- Page 67 and 68: Client-side JavaScript 64 implement
- Page 69 and 70: Client-side JavaScript 66 Reference
- Page 71 and 72: dhtmlx [1] Comparison of JavaScript
- Page 73 and 74: ility / ion Comparison of JavaScrip
- Page 75 and 76: Comparison of JavaScript frameworks
- Page 77 and 78: Comparison of JavaScript frameworks
- Page 79 and 80: Comparison of JavaScript-based sour
- Page 81 and 82: Comparison of JavaScript-based sour
- Page 83 and 84: Douglas Crockford 80 Douglas Crockf
- Page 85 and 86: DWR (Java) 82 DWR (Java) Developer(
- Page 87 and 88: EMVC 84 EMVC Developer(s) Ed Hertzo
- Page 89 and 90: Brendan Eich 86 References [1] Stev
- Page 91 and 92: JSDoc 88 JSDoc JSDoc is a syntax fo
JavaScript syntax 40<br />
Inheritance<br />
JavaScript supports inheritance hierarchies through prototyping in the manner <strong>of</strong> Self.<br />
In the following example, the Derive class inherits from the Base class. When d is created as a Derive, the reference<br />
to the base instance <strong>of</strong> Base is copied to d. base.<br />
Derive does not contain a value for aBaseFunction, so it is retrieved from Base when aBaseFunction is accessed.<br />
This is made clear by changing the value <strong>of</strong> base.aBaseFunction, which is reflected in the value <strong>of</strong> d.aBaseFunction.<br />
Some implementations allow the prototype to be accessed or set explicitly using the proto slot as shown below.<br />
function Base() {<br />
}<br />
this.anOverride = function() {alert("Base::anOverride()");};<br />
this.aBaseFunction = function() {alert("Base::aBaseFunction()");};<br />
function Derive() {<br />
}<br />
this.anOverride = function() {alert("Derive::anOverride()");};<br />
base = new Base();<br />
Derive.prototype = base; // Must be before new Derive()<br />
d = new Derive(); // Copies Derive.prototype to d instance's hidden<br />
prototype slot.<br />
base.aBaseFunction = function() {alert("Base::aNEWBaseFunction()")}<br />
d.anOverride(); // Derive::anOverride() d.aBaseFunction(); //<br />
Base::aNEWBaseFunction() alert(d.aBaseFunction ==<br />
Derive.prototype.aBaseFunction); // true<br />
alert(d. proto == base); // true in Mozilla-based implementations<br />
but false in many other implementations<br />
The following shows clearly how references to prototypes are copied on instance creation, but that changes to a<br />
prototype can affect all instances that refer to it.<br />
function m1() {return "One ";}<br />
function m2() {return "Two ";}<br />
function m3() {return "Three ";}<br />
function Base() {}<br />
Base.prototype.yyy = m2;<br />
bar = new Base();<br />
alert("bar.yyy " + bar.yyy()); // bar.yyy Two<br />
function Top(){this.yyy = m3}<br />
ttt = new Top();