Created
February 3, 2011 19:02
-
-
Save seanhess/809964 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| "use strict"; // strict mode doesn't work, but it WOULD need to come in at the top of the file | |
| // http://www.slideshare.net/kangax/say-hello-to-ecmascript-5 | |
| // http://davidflanagan.com/Talks/es5/slides.html | |
| // http://www.slideshare.net/BrendanEich/metaprog-5303821#text-version | |
| var sys = require("sys") | |
| // ARRAY | |
| var array = [1,2,3,4,5] | |
| sys.puts("Every Element Less than 5? " + array.every(function(element, index, array) { | |
| return element < 5 | |
| })) | |
| sys.puts("Any element less than 5? " + array.some(function(element, index, array) { | |
| return element < 5 | |
| })) | |
| sys.puts(array.indexOf(2)) | |
| sys.puts(array.lastIndexOf(2)) | |
| sys.puts(array.filter(function(item) { return item > 3 })) | |
| array.forEach(function(item) { sys.puts("FE " + item) }) | |
| sys.puts(array.map(function(item) { return item + 1})) | |
| sys.puts("Sum: " + array.reduce(function(prev, curr) { return prev + curr })) // also reduceRight | |
| // OBJECTS | |
| sys.puts("OBJECTS") | |
| var obj = {name:"value"} | |
| sys.puts(Object.keys(obj)) | |
| sys.puts(Object.getOwnPropertyNames(obj)) // also non-enumerable property names | |
| function Bob() {} | |
| Bob.prototype = {something:"hi"} | |
| sys.puts(Object.getPrototypeOf(new Bob()).something) | |
| // DATES | |
| sys.puts("DATES") | |
| var date = new Date() | |
| sys.puts(date.toISOString()) | |
| sys.puts(Date.parse("2010-01-01 12:00")) | |
| sys.puts(new Date("2010-01-01 12:00")) // saweet! | |
| sys.puts(Date.now()) // instead of (new Date()).getTime() | |
| // STRING | |
| sys.puts(" hello world ".trim()) | |
| // WEIRDER OBJECT STUFF | |
| var point = { x: 5, y: 8, get r() { return 3 } } | |
| sys.puts(sys.inspect(Object.getOwnPropertyDescriptor(point, 'x'))) | |
| sys.puts(sys.inspect(Object.getOwnPropertyDescriptor(point, 'r'))) | |
| Object.defineProperty(point, 'z', { | |
| value: 0, | |
| enumerable: true, // shows up in for (var i in obj)? | |
| writable: false, // disable obj.name = x | |
| configurable: false // diable delete obj.name | |
| }) | |
| var point = Object.create(Object.prototype, { | |
| x: { value: 5 }, | |
| y: { value: 8 }, | |
| r: { get: function() { return 2 }, enumerable:true }, | |
| z: { value: 0, enumerable: false }, | |
| asdf: { value: function() {} } // a value can be a normal function | |
| }) | |
| sys.puts(point.r) | |
| Object.preventExtensions(point) | |
| // point.woot = "hi" // throws an error! | |
| Object.seal(point) | |
| // delete point.x // throws an error! | |
| Object.freeze(point) | |
| // point.x = 7 // throws an error! | |
| // This is a BAD example | |
| function makePoint(x,y) { | |
| return {x:x, y:y} | |
| } | |
| function wrapPointWithLog(p) { | |
| return { | |
| get x() { | |
| sys.log('get x') | |
| return p.x | |
| }, | |
| set x(v) { | |
| sys.log('set x') | |
| p.x = v | |
| } | |
| // etc | |
| } | |
| } | |
| var wrappedPoint = wrapPointWithLog(makePoint(1,2)) | |
| wrappedPoint.x | |
| wrappedPoint.x = 4 | |
| // CLONE | |
| Object.defineProperty(Object.prototype, "clone", { | |
| value: function(props) { | |
| return Object.create(this, props) // not sure | |
| } | |
| }) | |
| var john = { | |
| name: "John", | |
| skill: "Javacsript" | |
| } | |
| var mike = john.clone({name: {value: "Mike"}}) | |
| sys.puts(mike.name) | |
| sys.puts(mike.skill) | |
| mike.name = "Bob" | |
| sys.puts(mike.name) | |
| sys.puts(john.name) // doesn't change john. | |
| // HARMONY PROXIES - don't work :( | |
| // This WOULD allow us to intercept missing methods and properties, etc. | |
| // function makeLogger(obj) { | |
| // var proxy = Proxy.create({ | |
| // get: function(rcvr, name) { | |
| // sys.log("Got " + name) | |
| // return obj[name] | |
| // }, | |
| // set: function(rcvr, name, val) { | |
| // sys.log("Set " + name) | |
| // obj[name] = val | |
| // return true | |
| // } | |
| // }, Object.getPrototypeOf(obj)) | |
| // return proxy | |
| // } | |
| // var loggedPoint = makeLogger(point) | |
| // sys.puts(ForwardingHandler) | |
| // loggedPoint.x = 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment