Peter van der Zee wrote:
> On Mon, Jun 13, 2011 at 5:44 AM, radiate <[email protected]> wrote:
> > Not sure if I misunderstood the purpose the prototype object of a
> > javascript object but I wanted to add functionality to an HTML Table
> > Cell that would generate a text box if a user clicked on the cell. I
> > wanted to provide this functionality to all table cells in the table.
> > I thought this would be a good use of the prototype functionality in
> > Javascript. It seems like I can't get it to work. Here's the code:
>
> Well, there are two reasons it's not really working or going to work like
> that.
>
> For one, in order to extend an object through prototype you have to
> extend the parent object's prototype property, not that of the child.
I find it more helpful to use terminology from the language
specifiction and say that instances inherit from the prototype of
their constructor, not from their own public prototype (if they have
one). The link from an instance to its constructor's prototype (i.e.
the one it inherits from) is described as its private [[Prototype]]
property.
> So.. if person is a child of Person:
>
> person.prototype.newMethod = function(){}; // NOT
> Person.prototype.newMethod = function(){}; // YES
It's only a convention that constructors start with a captial letter,
you are assuming that the OP knows that. Also, it's possible that
person.prototype references the same object as Person.prototype since
you haven't shown how either object was created. In the case of built-
in ECMAScript objects, some inherit from their own prototype (e.g.
Function, Object via Function.prototype) and some don't (Array,
String, etc.).
// Constructor - returns its this object by default
function Person(name) {
this.name = name;
// implied: return this;
}
// Method inherited by instances of Person
Person.prototype.sayName = function() {
alert(this.name);
}
// Create an instance of Person
var sue = new Person('Sue');
// Call a method
sue.sayName(); // shows Sue
In the above, the object sue has properties assigned in the
constructor and inherited from its constructor's prototype. It doesn't
have a prototype property.
// Add new methods
Person.prototype.setAge = function(age) {
this.age = age;
}
Person.prototype.getAge = function() {
return this.age;
}
sue.setAge(42);
alert(sue.getAge()); // 42
And so on.
>
> Note that the .prototype property of an instance (person) does not
> refer to "its prototype object", or Person.prototype. Formally you
> have no access to it through an instance but in most (not all!)
> browsers you can still find it through .__proto__ .
In most cases it can be found as constructor.prototype, though it is
not reliable as both the constructor and prototype properties are
public and can be easily changed.
alert(sue.constructor.prototype === Person.prototype); // true
However there is rarely a good reason to find an object's
[[Prototype]] (or __proto__).
> The other reason results might vary is because dom nodes are
> implemented using so called "host objects". These are special kinds of
> objects that typically don't adhere to the general rules of js. That
> means that in most cases, they behave as regular objects insofar as
> accessing properties go (and not even that, really) but extending them
> or creating new instances of them is all left up to the
> implementation.
Yes.
> Pretty much any object related to the DOM is a host
> object.
There are native objects (which include the built-in objects like
Object, Array, Math and so on and also those created using ECMAScript
such as functions, objects, strings, etc.). Anything else is a host
object (including everything to do with the DOM).
--
Rob
--
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]