5 ms·
You can create a prototype chain in Lua by associating an object with a metatable that has an __index metamethod. If the table that __index references has a met
by Twisol 11y ago
You can create a prototype chain in Lua by associating an object with a metatable that has an __index metamethod. If the table that __index references has a metatable itself, the chain continues.
http://www.lua.org/pil/13.4.1.html http://www.lua.org/pil/13.4.1.html
Importantly, `tbl[key]` only consults `__index` if `key` isn't actually a key in `tbl`, so keys in derived objects override keys up the prototype chain.
- david-given 11y agoAlso in Lua you can change the prototype of constructed objects, by just calling setmetatable() on them again; while in Javascript you can't --- once the object's been constructed, that's it. http://stackoverflow.com/questions/7015693/how-to-set-the-prototype-of-a-javascript-object-that-has-already-been-instantiat http://stackoverflow.com/questions/7015693/how-to-set-the-pr... I ran into this a little while back while trying to port some Lua code to Javascript. It was very annoying.
- masklinn 11y ago> while in Javascript you can't --- once the object's been constructed, that's it. ES2015 standardised the read/write __proto__ attribute (which most browsers had implemented for a long time), and added a #setPrototypeOf call. The first answer of your link notes exactly that.
- david-given 11y agoIt's added as an optional feature, as the first sentence of that answer describes: > ...the answer below is no longer accurate. _proto_ is being added to ecmascript6 as "normative optional" which means it isn't required to be implemented... It can't always be polyfilled, either, which makes the feature effectively useless. I'm not even sure why they added it as such a half-hearted feature to ES6; I suspect politics (Brendon Eich appears to have been dead set against having mutable protos for years).
- masklinn 11y ago> It's added as an optional feature Fuck's sake mate, Object.setPrototypeOf is not optional (and __proto__ is only optional for non-browser environments). > It can't always be polyfilled, either, which makes the feature effectively useless. That's breathtaking dishonesty.
- mikemike 11y agoJust in case, anyone has somehow gotten to the conclusion that Lua's semantics are 'simple', they should closely inspect this example and try to figure out through which contortions the VM has to go to make this work: local t = setmetatable({}, { __index = pcall, __newindex = rawset, __call = function(t, i) t[i] = 42 end, }) for i=1,100 do assert(t[i] == true and rawget(t, i) == 42) end [LuaJIT has no problems with this code and turns it into 8 machine code instructions for the actual loop.] Anyway ... This permanent excuse of JavaScript proponents that it has more complex semantics, which somehow prevents it from being made fast, is getting old. There are no insurmountable obstacles to make JavaScript fast -- it just takes more effort! And they dug this hole themselves, by not cleaning up the language and allowing new complicated features into the language. Well ...