I raised a similar question on es-discuss before. The main reason is separation of meta- and normal- levels of objects usage. Discussion is here: https://mail.mozilla.org/pipermail/es-discuss/2010-April/010908.html

Some languages (including JS) used unstratified meta-programming -- the technique which syntactically (and probably ideologically) makes programming easier. Actually, it's arguable what should be stratified to the meta-level. E.g. a string representation of an object -- is it meta- or normal- level?

JS: foo.toString();

Python: str(foo)

Or, dynamic property name (i.e. not known in advance and formed in runtime) -- is it a meta operation or a normal?

JS: foo["b" + "ar"]()

Python: getattr(foo, "b" + "ar")()

Or a number -- can _it_ (by _itself_) get its rounded value, or this is a meta (? or even, just a function of module) operation?

JS: Math.round(3.4)

Ruby: 3.4.round

If an object (in JS) can get _its_ string representation _by itself_, why can't it get its keys then -- i.e. `obj.keys`? Why this should be moved to the meta-level -- `Object.keys(foo)`?

With this exact example with `Object.keys` -- it was borrowed from Prototype.js to ES5 (the same as Function#bind). The most funny thing, that Prototype.js also borrowed it from Ruby, where this method is instance for `Hash` class, i.e. Hash#keys, as we can use there `foo.keys`. However, Prototype.js couldn't place it directly on `Object.prototype` since couldn't control enumerable attribute, therefore they place in on `Object` (if they could -- they I guess would place it on `Object.prototype` as they done with `Function#bind` and with other monkey-patched methods).

So just borrowing Ruby-style short-names as `Object.keys` and mixing them with Java-style long-long-sentences as `Object.getOwnPropertyNames` I think is a lack and is the same as PHP couldn't decide how to name their global functions -- with underscore or without it (though, largely it doesn't matter much in practical programming).

So currently ES5 has mixed stuff from several designs: e.g., `foo.isPrototypeOf(bar)`, vs. `Object.getPrototypeOf(foo) == bar` (one meta-method is an instance method, the second one -- static class method), `Object.keys` vs. `Object.getOwnPropertyNames` (one from Ruby-style for naming convention, the second one -- from Java-style), etc.

Generally, stratified met-programming is nevertheless good point, I think. That's the reason, why magic Python's __names__ are also moved to the meta-level, though still can be easily emulated back if needed for the convenient programming in own project, e.g. http://bit.ly/gwZtNO. But also a convenient wrapper can be used to work with stratified meta-level, e.g. http://bit.ly/dJZHF0.

So ES5 have chosen this way and as is said, it's good way of stratification. In the own project, though as is also said, we may always provide aliases for the `Object.prototype` (since now we can control enumerable/configurable state): https://gist.github.com/367080/

Dmitry.

On 01.03.2011 8:26, Michael Haufe (TNO) wrote:
It doesn't make sense on Object.prototype. For example:

Object.prototype.keys = function(){
     var ret=[],p;
     for(p in this)
         if(Object.prototype.hasOwnProperty.call(this,p))
             ret.push(p);
     return ret;
}

function Employee(name){
     this.name = name;
}
Employee.prototype = {
     sayHello : function(){
        return "Hello! My name is "+ this.name;
     },
     job : "Engineer"
}

var p = new Employee("Peter");

console.log(p.keys().join());
---------------------------------------------------

Also, what would be the point of using it on an Array?

var p = ['a','b','c'];

console.log(p.keys().join());  // 0,1,2

--------------------------------------------------

Or a regular function?

function foo() { this + " [stuff]" }

console.log(foo.keys().join());

--------------------------------------------

There is no point in augmenting every object in the language with
method(s) that have no meaning in that object's context

On Feb 28, 8:56 pm, Jason Persampieri<ja...@persampieri.net>  wrote:
Just read Angus Croll's post on the new Object properties, 'keys' and
'getOwnPropertyNames'.

http://javascriptweblog.wordpress.com/2011/02/28/javascript-object-ke...

It got me thinking... I hadn't noticed until now, but it looks like all of
the new properties (create/preventExtensions/etc) are implemented on Object,
as opposed to Object.prototype.  Any insight as to why?  It may be just a
personal preference, but I would *vastly* prefer obj.keys() over
Object.keys(obj).

_jason

--
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to