Last active
August 29, 2015 14:02
-
-
Save zeusdeux/cceee7dea9efd132429c to your computer and use it in GitHub Desktop.
Playing with Object.observe ( try it in Canary here -> http://experiments.muditameta.com/objectObserve )
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
function Circle(r){ | |
var self = this; | |
var notifier = Object.getNotifier(this); | |
Object.defineProperty(this, "radius", { | |
get: function(){ | |
return r; | |
}, | |
set: function(newR){ | |
if (r === newR) | |
return; | |
r = newR; | |
notifier.notify({ | |
name: "radius", | |
type: "radiusSet", | |
newRadius: newR | |
}); | |
} | |
}); | |
Object.defineProperty(this, "area", { | |
get: function(){ | |
return Math.PI*Math.pow(r, 2); | |
}, | |
set: function(newA){ | |
notifier.notify({ | |
name: "area", | |
type: "radiusChanged", | |
newRadius: Math.sqrt(newA/Math.PI) | |
}); | |
} | |
}); | |
Object.defineProperty(this, "circum", { | |
get: function(){ | |
return 2*Math.PI*r; | |
}, | |
set: function(newC){ | |
notifier.notify({ | |
name: "circum", | |
type: "radiusChanged", | |
newRadius: newC / (2*Math.PI) | |
}); | |
} | |
}); | |
Object.observe(this, function(changeRecs){ | |
self.radius = changeRecs[0].newRadius; | |
}, ["radiusChanged"]); | |
}; |
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
<html> | |
<head> | |
<style> | |
input[type="text"] { | |
display: block; | |
margin: 10px; | |
} | |
</style> | |
</head> | |
<body> | |
<label>Radius: | |
<input type="text" name="radius" id="radius"> | |
</label> | |
<label>Circumference: | |
<input type="text" name="circum" id="circum"> | |
</label> | |
<label>Area: | |
<input type="text" name="area" id="area"> | |
</label> | |
<script src="./Circle.js" /> | |
<script src="./main.js" /> | |
</body> | |
</html> |
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
window.onload = function(){ | |
var c1 = new Circle(5); | |
var rad = document.querySelector('#radius'); | |
var circum = document.querySelector('#circum'); | |
var area = document.querySelector('#area'); | |
function update(){ | |
rad.value = c1.radius; | |
circum.value = c1.circum; | |
area.value = c1.area; | |
} | |
[].forEach.call(document.querySelectorAll('input[type="text"]'), function(v){ | |
v.addEventListener('keyup',function(e){ | |
c1[e.target.id] = this.value; | |
}); | |
}); | |
Object.observe(c1, function(){ | |
update(); | |
}, ["radiusSet"]); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment