Last active
May 4, 2016 16:17
-
-
Save thatseeyou/fed7f49e7b9c4ffe17d4 to your computer and use it in GitHub Desktop.
JavaScript Exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.prototype.extendedValue = "extended"; | |
var obj1 = { | |
prop1: "value1", | |
prop2: "value2" | |
}; | |
var property; | |
for (property in obj1) { | |
console.log(property + ":" + obj1[property]); | |
} | |
console.log("----"); | |
var properties = Object.keys(obj1); | |
var i, len; | |
for (i = 0, len = properties.length; i < len; i++) { | |
var property = properties[i]; | |
console.log(property + ":" + obj1[property]); | |
} | |
/* | |
prop1:value1 | |
prop2:value2 | |
extendedValue:extended | |
---- | |
prop1:value1 | |
prop2:value2 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Not using prototype | |
function View(x, y) { | |
this.x = x; | |
this.y = y; | |
this.redraw = function() { | |
console.log("redraw at (" + this.x + "," + this.y + ")"); | |
}; | |
} | |
var v1 = new View(1,2); | |
v1.redraw(); | |
var v2 = new View(10,20); | |
v2.redraw(); | |
v1.redraw !== v2.redraw && console.log("not same"); | |
// Using prototype | |
function View2(x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
View2.prototype.redraw = function() { | |
console.log("redraw at (" + this.x + "," + this.y + ")"); | |
}; | |
var v3 = new View2(1,2); | |
v3.redraw(); | |
var v4 = new View2(10,20); | |
v4.redraw(); | |
v3.redraw === v4.redraw && console.log("same"); | |
/* | |
redraw at (1,2) | |
redraw at (10,20) | |
not same | |
redraw at (1,2) | |
redraw at (10,20) | |
same | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment