Objects
Objects
Objects
Manu Dube
Objects
• JavaScript has primitives and objects as data-types
–it is unordered
–configurable: whether the property can be deleted or its attributes can be altered
Properties can be
•
● property names that include spaces or hyphens, or are JavaScript keywords should be in quotes
var book = {
•
author: {
firstname: "Me",
lastname: "Myself"
};
var o2 = Object.create(null);
• //o2 does not inherit any properties or methods, not even basic ones
var o3 = Object.create(Object.prototype);
• // empty object, same as using {} or new Object()
<script>
var o1 = Object.create({x:0, y:1}, {r: {value: "p"}, y: {value:"q"}});
document.write(o1.x + "<BR>"+o1.y+ "<BR>"+o1.__proto__.y);
•</script>
–useful if wish to loop over properties such as address1, address2, ... or with for/in
<script>
var o1 = {a:5};
o1.x =1;
o1["y"]=2;
document.write(o1.a+"<BR>");
document.write(o1.x + "<BR>"+o1.y);
</script>
–if you delete property from prototype object, all objects that inherit from it are affected
–properties of global objects including variable and function definitions are nonconfigurable
<script>
var o1 = {a:5};
o1.x =1;
o1["y"]=2;
var o2 = Object.create(o1);
var o3 = Object.create(o1);
document.write(delete o3.y+"<BR>");
document.write(delete o2+"<BR>");
delete o3.__proto__.x;
document.write(o2.a+"<BR>");
document.write((o2.x != undefined)?o2.x:"hello"+ "<BR>"+o1.y);
</script>
–in operator returns true if object has its own, or an inherited property by the name
<script>
var o1 = {a:5};
o1.x =1;
var o2 = Object.create(o1);
o1["y"]=2;
document.write("a in o1: " + ("a" in o1) + "<BR>");
document.write("a in o2: " + ("a" in o2) + "<BR>");
document.write("a own in o1: "+ o1.hasOwnProperty("a") + "<BR>");
document.write("a own in o2: "+ o2.hasOwnProperty("a") + "<BR>");
document.write("y in o1: " + ("y" in o1) + "<BR>");
document.write("y in o2: " + ("y" in o2) + "<BR>");
document.write("y own in o1: "+ o1.hasOwnProperty("y") + "<BR>");
document.write("y own in o2: "+ o2.hasOwnProperty("y") + "<BR>");
document.write("deleting a in o1" + delete o1.a+"<BR>");
document.write("a in o1: " + ("a" in o1) + "<BR>");
document.write("a in o2: " + ("a" in o2) + "<BR>");
document.write("deleting y in o2" + delete o2.y+"<BR>");
document.write("y in o1: " + ("y" in o1) + "<BR>");
document.write("y in o2: " + ("y" in o2) + "<BR>");
</script>
–if you modify properties while constructing object via Object.create, make it enumerable
<script>
var o1 = {a:5, b:13, c:"hello"};
if("toString" in o1) document.write("toString is a property <BR>");
for (p in o1) document.write(p + ": " + o1[p] + "<BR>");
var o2 = Object.create(o1, {a: {value: 99}, c: {value: "hi!", enumerable: true}});
for (p in o2) document.write(p + ": " + o2[p] + "<BR>");
</script>
–the first is for enumerable, the second also lists nonenumerable but not inherited
<script>
var o1 = {a:5, b:13, c:"hello"};
var o2 = Object.create(o1, {a: {value: 99}, c: {value: "hi!", enumerable: true}});
document.write(Object.keys(o1));
document.write("<BR>");
document.write(Object.getOwnPropertyNames(o2));
</script>
<script>
var o1 = {a:5, b:13, c:"hello", d:function(){document.write("Hello World!")}};
o1.d();
o1.e=function(){document.write("Bye!")};
o1.e();
o1.d = function(){document.write("Now Goodbye")};
o1.d();
delete o1.d;
document.write(typeof o1.d);
</script>
<script>
var myob = {
x:5,
y:10,
dist: function(){return Math.sqrt(this.x*this.x+this.y*this.y);}
}
document.write(myob.dist());
</script>
–for accessor properties rather than simply data properties that have a value
–such properties do not have a writable attribute, but are read if getter is defined and write if setter is
<script>
var o1 = {
x:5,
get a() {return this.x*this.x;},
set a(n) {this.x=Math.sqrt(n);}
};
document.write(o1.x);
document.write("<BR>");
document.write(o1.a);
document.write("<BR>");
o1.a = 9;
document.write(o1.x);
</script>
<script>
var o1 = {};
Object.defineProperty(o1, "x", {value: 5, enumerable: true, writable: true, configurable: true});
Object.defineProperty(o1, "a", {get: function(){return this.x*this.x},
set: function(n){this.x=Math.sqrt(n);}});
document.write(o1.x);
document.write("<BR>");
document.write(o1.a);
document.write("<BR>");
o1.a = 9;
document.write(o1.x);
document.write("<BR>");
document.write(Object.getOwnPropertyDescriptor(o1, "x"));
document.write("<BR>");
document.write(Object.getOwnPropertyDescriptor(o1, "a"));
document.write("<BR>");
document.write(Object.getOwnPropertyDescriptor(o1, "y"));
document.write("<BR>");
</script>
<script>
var o1 = {};
</script>
<script>
function doNothing(a,b,c){
this.work=a,
this.urgent=b,
this.mustdo=c,
this.toString=function(){return this.work+" "+this.urgent+" "+this.mustdo+"<BR>"}
}
<script>
function doNothing(a,b,c){
this.work=a,
this.urgent=b,
this.mustdo=c
}
useful.x = 4;
document.write(useless.x);
</script>
<script>
function doNothing(a,b,c){
this.work=a,
this.urgent=b,
this.mustdo=c
}
document.write(useful == useless);
document.write("<BR>");
document.write(useful == copycat);
</script>
<script>
function doNothing(a,b,c){
this.work=a,
this.urgent=b,
this.mustdo=c
}
var useless = new doNothing("nothing", "not at all", {must: "why", do: "not!"});
for(var i in useless)
document.write(i+" "+useless[i]+"<BR>");
</script>
function doNothing(){};
document.write(typeof doNothing.prototype);
<script>
function doNothing(){this.work="nothing"};
for(var i in doNothing.prototype)
var a = new doNothing();
document.write("<BR>"+i+doNothing.prototype[i]);
document.write(a.work+"<BR>");
document.write("hello");
var b = doNothing();
document.write(b+"<BR>");
doNothing.prototype.sing = function(){document.write("<BR>
document.write(work);
Hello World <BR>")};
</script>
var a = new doNothing();
a.sing();
</script>
<script>
function doNothing(){};
a.sing();
b.sing();
</script>
<script>
function doNothing(){};
doNothing.prototype.x = 15;
doNothing.prototype.y = 16;
var a = new doNothing();
a.x = 17;
document.write(a.x+" "+a.y);
if("x" in a) document.write("x is in a <BR>");
if("y" in a) document.write("y is in a <BR>");
if("z" in a) document.write("z is in a <BR>");
if(a.hasOwnProperty("x")) document.write("x own <BR>");
if(a.hasOwnProperty("y")) document.write("y own <BR>");
if(a.hasOwnProperty("z")) document.write("z own <BR>");
if(a.propertyIsEnumerable("x")) document.write("x is Enumerable <BR>");
if(a.propertyIsEnumerable("y")) document.write("y is Enumerable <BR>");
if(a.propertyIsEnumerable("z")) document.write("z is Enumerable <BR>");
</script>
<script>
function doNothing(a,b,c){
this.work=a,
this.urgent=b,
this.mustdo=c,
this.sing = function(){document.write("hello world");}
}
doNothing.prototype.shout=function(){document.write("HELLO WORLD");}
document.write(useful.hasOwnProperty("sing"));
document.write("<BR>");
document.write(useful.hasOwnProperty("shout"));
</script>
• Can check using instanceof operator to see which constructor was used
• Can use isPrototypeOf() to look up prototype in chain
• Can use getPrototypeOf() to get the prototype of the object
<script>
function doNothing(a,b,c){
this.work=a,
this.urgent=b,
this.mustdo=c
}
<script>
Shape.prototype = {
area: 0,
perimeter: 0,
print: function(){document.write(this.area+ " " + this.perimeter+"<BR>");}
}
var a = Object.create(Shape.prototype);
a.print();
</script>
Look it up yourself!
•
–toString()
–toLocaleString()
–toJSON
–valueOf()