On Mon, Feb 28, 2011 at 9:26 PM, Michael Haufe (TNO) <
[email protected]> 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());
>
this outputs the same thing as Object.keys(p). Taken by itself, this
doesn't convince me.
> ---------------------------------------------------
>
> 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
>
> --------------------------------------------------
>
Admittedly this seems weird. More below.
> Or a regular function?
>
> function foo() { this + " [stuff]" }
>
> console.log(foo.keys().join());
>
> --------------------------------------------
>
Not as convinced this is as weird. What's wrong with enumerating the
properties on a function? What if the method in question were called
'properties' instead? Would that make this less weird to you?
> There is no point in augmenting every object in the language with
> method(s) that have no meaning in that object's context
>
For a language where "everything is an object" (a construct I love), then it
seems odd to differentiate between what is, in essence, a hash (which
Object.keys is made for) and all other objects. I'd rather be able to call
the method in 'weird' ways than to start separating those.
And what about all the other new ES5 methods: create, freeze,
preventExtensions, etc? These all seems perfectly reasonable to apply to
arrays, functions, etc.
That said, if this is inherently a decision to help interpreter performance
or memory usage, then I really can't argue against that.
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]