5

My expectation from the following code would be that if I checked a.name, it would search the prototype and return it as it was declared. Can anyone pinpoint what it is that is preventing JS from acknowledging my prototype?

var obj = function(parent){
    return {
        prototype: parent
    }
};

var me = { name: 'keith' };

var a = new obj(me)
// => undefined

a.name
// => undefined

a.prototype.name
// => "keith"
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
netpoetica
  • 3,375
  • 4
  • 27
  • 37

5 Answers5

1

In the function you are not touching the function prototype. You are just returning a new object with property "prototype" with parent as a value. Therefore you can only access it through that property.

It seems that you are trying to implement inheritance, here's a good read about that: Prototypal Inheritance in JavaScript and Classical Inheritance in JavaScript

valentinas
  • 4,277
  • 1
  • 20
  • 27
  • Hey, you're right, I am trying for inheritance here. Thanks for those articles. I've read both thoroughly but I just think it's ugly to have to use a function that returns your object without using "new" in its creation. It's just a little unreadable in my opinion. Was hoping to find a way to make using "new" a possibility – netpoetica Oct 26 '12 at 04:44
1

A property named "prototype" is just a property, it does not point to the object from which the object inherits. Use Object.getPrototypeOf or the non-standard __proto__ property to get that.

So what your function obj(me) returns is just an object with a property "prototype" which points to an object with a property "name" which points to the string keith. As your function returns an object, it makes no difference whether it is called with the new keyword or not.

For inheritance, the "prototype" property of the constructor function [object] is of concern. Every object created by this constructor (which does not return an object) with the new keyword inherits from the object to which the "prototype" property of the constructor points. So you could do this:

var Constructor = function() {
    console.log(this); // logs the currently created object
    // return nothing
}
Constructor.prototype = { name: 'keith' };

var a = new Constructor(); // logs an empty object
Object.getPrototypeOf(a) === Constructor.prototype; // true
a.name; // "keith" - inherited
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

I suspect you're overwriting the prototype property in a similar way to how you can overwrite the undefined "value".

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

The problem here is that you are returning a property named prototype with the value parent, which is an object, so you are returning the pair prototype = {name = 'keith'} and when you call new obj, you are adding to the prototype of a a new property named prototype.

You just need a bit change, I need this is what you are trying to do. This will work, just be carefull with overload properties.

var obj = function(parent){
   for(var propt in parent){
      this[propt] = parent[propt];
   }
}

var me = { name: 'keith' };
var a = new obj(me);
console.log(a);
// => Object { name="keith"}
console.log(a.name);
// => "keith"

Edit: If you are looking for inheritance using prototype, you should read this or try TypeScript, a new javascript typed and OO library

Community
  • 1
  • 1
Tomas Ramirez Sarduy
  • 17,294
  • 8
  • 69
  • 85
  • This is really just cloning properties from one object to another, whereas I'd ultimately like to be able to use "prototype" as one would use "super" in a classical language. This would work, but it isn't quite inheritance :-) – netpoetica Oct 26 '12 at 04:46
  • you didn't mention the inheritance. You are refering to the `super` keyword in `java` right? – Tomas Ramirez Sarduy Oct 26 '12 at 04:54
-1

You seem to have overwritten the real prototype with a fake one.

rodmjay
  • 549
  • 4
  • 15
  • he just added a new property to `obj.prototype` named prototype – Tomas Ramirez Sarduy Oct 26 '12 at 04:44
  • I think Rod is actually correct, it looks like somehow I abolished the internal link to "prototype" and replaced it with a property that JavaScript doesn't implement the prototype functionality on. I think it may be that the new keyword is initializing the object first, and *then* overwriting prototype with a new value, which is no longer in the scope chain – netpoetica Oct 26 '12 at 04:49