Revisiting JavaScript Objects
laktek.com
laktek.com
> var F = function () {};
> F.prototype = null;
null
> (new F).__proto__ === Object.prototype;
true
This means all the "default" object methods are there (e.g. #hasOwnProperty) and ready to get clobbered with all the risk it entails[0].With ``Object.create(null)``, however, the __proto__ of the created object is effectively ``null``:
> Object.create(null).__proto__
null
and such pesky methods as #hasOwnProperty or #toString are not there for clobbering.And an issue of Object.create failed to be mentioned as well: you can't use ``instanceof`` directly, you have to create one-off "constructor functions" to call ``instanceof`` on them.
[0] http://www.devthought.com/2012/01/18/an-object-is-not-a-hash...
- JavaScript looks a bit like (Java/Ruby/other language)
- JavaScript does not work like (said language)
- JavaScript sucks
Granted, I appreciate folks pointing out the "bad parts" of the language and the strange gotchas. And JavaScript has more than its share of bad parts. But it also requires a change in mindset. At minimum, expect
- a priority of first-class functions,
- Prototypal rather than classical inheritance,
- time spent learning JavaScript syntax itself instead of constructs from other languages (which often will work, but not in the way expected).
Books that have given me a renewed appreciation are Douglas Crockford's book (JavaScript the Good Parts) and John Resig's "Secrets of the JavaScript Ninja."
Meh. Javascript looks, feels and behaves much more like a single-inheritance class-based language (without the class sugar) than like a prototype-based one (Io and even more so Self). Especially if you stick to the standard.
A small concession was made towards prototypal use in ES5 with Object.create (which remains little used and slow as molasses) but it's looking to snap back to classes with ES6
[1] http://javascript.spec.whatwg.org/#object.prototype.__proto_...
> Object.freeze(square);
> square.length = 20
20
> square.length
10
square.length = 20 should be throwing an error. No excuses, sane defaults. "use strict";
var square = {length:10};
Object.freeze(square);
square.length = 20;
C:\Users\Matt\Code\seal.js:5
square.length = 20;
^
TypeError: Cannot assign to read only property 'length' of #<Object>
at Object.<anonymous> (C:\Users\Matt\Code\seal.js:5:15)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)Knowing the [Good Parts](http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockfor...) helps but you just can't keep adding parts that you think may be good.
http://www.infoq.com/interviews/ecmascript-5-caja-retrofitti...
Many of the additions (including object.freeze) and changes were made for the purpose of making JavaScript usable as an object capability language. Mark Miller explains the background and some of the changes in the video linked above.
I have seen various definitions of the term "clone", e.g. Io language documentation says that "a clone is an empty object that has the parent in its list of protos", this article defines clone as an exact shallow copy of another object.
Neither. In Self lingo, A is a mixin of B. The least bad wording would be centered around composition, but I guess it could also be understood that B is an extension of A.
It can't be a clone or a copy, because changing A will still impact B, they don't become independent objects.