// CSE 341, Autumn 2010, Marty Stepp // lecture code about JavaScript objects, prototypes, and inheritance // a "constructor" function Point(xx, yy) { this.x = xx; this.y = yy; } Point.prototype.distanceFromOrigin = function() { return Math.sqrt(this.x * this.x + this.y * this.y); }; Point.prototype.toString = function() { return "(" + this.x + ", " + this.y + ")"; }; function Point3D(xx, yy, zz) { this.x = xx; this.y = yy; this.z = zz; } Point3D.prototype = new Point(); // extends Point Point3D.prototype.distanceFromOrigin = function() { return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z); }; Point3D.prototype.toString = function() { return "(" + this.x + ", " + this.y + ", " + this.z + ")"; }; var p1 = new Point(4, 7); var p2 = new Point(8, 19); var p3d = new Point3D(1, 2, 3); /* Rhino interactions Rhino 1.7 release 2 2010 01 20 js> load("lecture26.js"); js> p1 (4, 7) js> p2 (8, 19) js> function f() { print(this); } js> f(); [object global] js> var o = {}; js> o.foo = f; function f() { print(this); } js> o.foo(); [object Object] js> f(); [object global] js> new f() [object Object] [object Object] js> var p3 = Point(7, 16); js> p3 js> typeof(p3) undefined js> x 7 js> y 16 js> distanceFromOrigin function () { return Math.sqrt(this.x * this.x + this.y * this.y); } js> distanceFromOrigin() 17.46424919657298 js> var o1 = {a:1, b:2, f:function() { return 42; }}; js> var o2 = {c:3, g:function() { return 99; }}; js> o2.prototype = o1; [object Object] js> o2.f js> o2.f() js: "", line 23: uncaught JavaScript runtime exception: TypeError: Cannot find function f in object [object Object]. at :23 js> o2.prototype = {a:1, b:2, f:function() { return 42; }}; [object Object] js> o2.f() js: "", line 25: uncaught JavaScript runtime exception: TypeError: Cannot find function f in object [object Object]. at :25 js> function foo() {} js> foo.prototype [object Object] js> Point function Point(xx, yy) { this.x = xx; this.y = yy; this.distanceFromOrigin = function () { return Math.sqrt(this.x * this.x + this.y * this.y); }; this.toString = function () { return "(" + this.x + ", " + this.y + ")"; }; } js> Point.prototype [object Object] js> Point.prototype.booyah = function() { print("booyah!"); } function () { print("booyah!"); } js> var p4 = new Point(99, 44); js> p4.booyah(); booyah! js> stepp@stepp-thinkpad:~/341/lectures/files$ rhino Rhino 1.7 release 2 2010 01 20 js> p1 js: "", line 2: uncaught JavaScript runtime exception: ReferenceError: "p1" is not defined. at :2 js> load("lecture26.js"); js> p1 (4, 7) js> p2 (8, 19) js> p1.toString function () { return "(" + this.x + ", " + this.y + ")"; } js> p1.distanceFromOrigin function () { return Math.sqrt(this.x * this.x + this.y * this.y); } js> p1.distanceFromOrigin() 8.06225774829855 js> stepp@stepp-thinkpad:~/341/lectures/files$ rhino Rhino 1.7 release 2 2010 01 20 js> load("lecture26.js"); js> p1 (99999, -11111) js> p2 (99999, -11111) js> p1.distanceFromOrigin() 100614.38426984484 js> p1.x = 2; 2 js> p1.y = 4; 4 js> p2 (99999, -11111) js> p1 (2, 4) js> var s = "hello"; js> s.toUpperCase() HELLO js> String.prototype js> String.prototype.toUpperCase function toUpperCase() { [native code for String.toUpperCase, arity=0] } js> String.prototype.toUpperCase = null; null js> s.toUpperCase() js: "", line 15: uncaught JavaScript runtime exception: TypeError: Cannot call property toUpperCase in object hello. It is not a function, it is "object". at :15 js> Point function Point(xx, yy) { } js> Point.abc = 123; 123 js> Point.toString() function Point(xx, yy) { } js> Function.prototype function () { [native code, arity=0] } js> Function.prototype.booyah = function() { print("booyah!"); } function () { print("booyah!"); } js> Point function Point(xx, yy) { } js> Point.booyah() booyah! js> p1.booyah() js: "", line 23: uncaught JavaScript runtime exception: TypeError: Cannot find function booyah in object (2, 4). at :23 js> Object.prototype.toString = function() { return "AAAAASSSSSSS"; } function () { return "AAAAASSSSSSS"; } js> var o = {}; js> o AAAAASSSSSSS js> var o = {toString: function() { return "hi"; }; js: "", line 27: missing } after property list js: var o = {toString: function() { return "hi"; }; js: ..............................................^ js> var o = {toString: function() { return "hi"; }}; js> o hi js> stepp@stepp-thinkpad:~/341/lectures/files$ rhino Rhino 1.7 release 2 2010 01 20 js> load("lecture26.js"); js> p3d (1, 2) js> p3d.distanceFromOrigin function () { return Math.sqrt(this.x * this.x + this.y * this.y); } js> */