By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,126 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,126 IT Pros & Developers. It's quick & easy.

How to make a Class method for initialed obj?

P: n/a
I have the following code:

var obj = {a:0, b:1, ...}

function f() {...}
obj.f = f // This will make a instance method?

How to make a Class method for the class of obj? Or what's the class of
"obj"?
Jul 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Nick <nb**********@hotmail.com> writes:
I have the following code:

var obj = {a:0, b:1, ...}

function f() {...}
obj.f = f // This will make a instance method?
It will make the function named f a property of the object referred to
by the variable obj. We use the word "method" merely as a shorthand
for "property which is a function". In that sense, it is a method
of "obj".
How to make a Class method for the class of obj? Or what's the class
of "obj"?


There are no classes in Javascript. It is a prototype based language,
and inheritance happens between objects, not between "classes".

The way Javascript implements this inheritance is through helper
functions which can also be used to initialize new objects.

The "obj" is not a class, nor is it an instance of a class, since
there are no classes. It is created using a literal, so it is not even
constructed using a constructor function (although it behaves like it
was created using the Object constructor function).

--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
> I have the following code:

var obj = {a:0, b:1, ...}

function f() {...}
obj.f = f // This will make a instance method?

How to make a Class method for the class of obj? Or what's the class of
"obj"?


In your example, f is a method of obj. You could have written it more
compactly:

var obj = {a: 0, b: 1, ..., f: function () {}};

JavaScript is a Class-Free language, but it is possible to use it in the
old style. See http://www.crockford.com/javascript/inheritance.html.
Jul 20 '05 #3

P: n/a
According to the book "Javascript The Definitive Guide" (O'Reilly), a
Class constructor can be done. For example,

function Circle(radius) {
this.r = radius;
}

And Class variable/function can be done:
Circle.PI = 3.14159;
function Circle_max(a,b) { ... }
Circle.max = Circle_max;

And instance method is a little bit cumbersome:

function Circle_area() { ... };
Circle.prototype.area = Circle_area;

To instanize a circle, just do,

var c = Circle(23.0);

So it looks not clear that what the class of an initialized object is.
i.e. var obj = { a:1, b:2, f:function () {...}, .... }
Lasse Reichstein Nielsen wrote:
Nick <nb**********@hotmail.com> writes:

I have the following code:

var obj = {a:0, b:1, ...}

function f() {...}
obj.f = f // This will make a instance method?

It will make the function named f a property of the object referred to
by the variable obj. We use the word "method" merely as a shorthand
for "property which is a function". In that sense, it is a method
of "obj".

How to make a Class method for the class of obj? Or what's the class
of "obj"?

There are no classes in Javascript. It is a prototype based language,
and inheritance happens between objects, not between "classes".

The way Javascript implements this inheritance is through helper
functions which can also be used to initialize new objects.

The "obj" is not a class, nor is it an instance of a class, since
there are no classes. It is created using a literal, so it is not even
constructed using a constructor function (although it behaves like it
was created using the Object constructor function).

Jul 20 '05 #4

P: n/a
Nick <nb**********@hotmail.com> writes:
According to the book "Javascript The Definitive Guide" (O'Reilly), a
Class constructor can be done. For example,
You can do something that in *some* ways resemble a class, but it
isn't. The Javascript "class" (i.e., a constructor function) is better
equated to a Java constructor than to a class. For instance, you
*can't* do inheritance between "classes" (constructor functions).
Inheritance happens between objects in Javascript, not "classes".
So, if anything corresponds to a Java class, it is the prototype,
not the constructor.

Try this:
---
function Foo(){}
function Bar(){};
var foo = new Foo();
var bar = new Bar();
Bar.prototype=Foo.prototype;
alert([foo instanceof Foo,
foo instanceof Bar,
bar instanceof Foo,
bar instanceof Bar]);
---
Which four booleans do you expect it to alert?
It alerts
true, true, false, false

Whoa! The "bar" object is no longer an instance of the "class"
function that constructed it. The "foo" object is an instance of two
different "classes". In fact, foo is an "instance" of Foo.prototype
(i.e., has it as a prototype), no matter where you store that
object. Inheritance has nothing to do with the Foo and Bar functions,
they just facilitate the creation of objects.

function Circle(radius) {
this.r = radius;
}
This is a constructor function. It is used to create objects with a
common (empty) prototype (Circle.prototype).
And Class variable/function can be done:
Circle.PI = 3.14159;
That should correspond to Java's static fields in that they are
properties of the "class".
function Circle_max(a,b) { ... }
Circle.max = Circle_max;
And a static/class method.

So far the metaphor holds.
And instance method is a little bit cumbersome:

function Circle_area() { ... };
Circle.prototype.area = Circle_area;
There is not a huge difference between that and
function Circle() {
...
this.area = Circle_area;
}
when you think in class-based patterns. It is only in the prototype
based view that there is a difference between a function in the object
itself and in the prototype.
To instanize a circle, just do,

var c = Circle(23.0);
Yes, that creates an object. You can think of Circle as its class, but
that's all in your head (and it can be a useful way to think too). But
in the language, there is no class.
So it looks not clear that what the class of an initialized object is.
i.e. var obj = { a:1, b:2, f:function () {...}, .... }
There is no class. The object is an "instanceof" the constructor
function Object, but all (non-host) objects are. Its "constructor"
property also points to the Object function.

Thinking in prototypes, it is simple what happens: The new object
is created. Its prototype is the original value of Object.prototype.
That's the entire story.
More func with "inheritance":
---
function Foo(){}
var foo = new Foo();

function Bar(){}
Bar.prototype = foo;
var bar = new Bar();

function Baz(){}
Baz.prototype=bar;
var baz = new Baz();

Bar.prototype = new Object();
alert([baz instanceof Baz, baz instanceof Bar, baz instanceof Foo);
---
Guess the result :)
Lasse Reichstein Nielsen wrote:


Please don't top post.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

P: n/a
One correction:

function Foo(){}
function Bar(){};
var foo = new Foo();
var bar = new Bar();
Bar.prototype=Foo.prototype;
alert([foo instanceof Foo,
foo instanceof Bar,
bar instanceof Foo,
bar instanceof Bar]);
---
shows: true, true, false, true
instead of: true,true,false, false

Anyway the O'Reilly book use term Class, which may not be proper for
prototype based OO language.

So the answer of my question should be,

Object obj = { .... } is and only is an instance of Object.

Is it a correct statement?

Lasse Reichstein Nielsen wrote:
Nick <nb**********@hotmail.com> writes:

According to the book "Javascript The Definitive Guide" (O'Reilly), a
Class constructor can be done. For example,

You can do something that in *some* ways resemble a class, but it
isn't. The Javascript "class" (i.e., a constructor function) is better
equated to a Java constructor than to a class. For instance, you
*can't* do inheritance between "classes" (constructor functions).
Inheritance happens between objects in Javascript, not "classes".
So, if anything corresponds to a Java class, it is the prototype,
not the constructor.

Try this:
---
function Foo(){}
function Bar(){};
var foo = new Foo();
var bar = new Bar();
Bar.prototype=Foo.prototype;
alert([foo instanceof Foo,
foo instanceof Bar,
bar instanceof Foo,
bar instanceof Bar]);
---
Which four booleans do you expect it to alert?
It alerts
true, true, false, false

Whoa! The "bar" object is no longer an instance of the "class"
function that constructed it. The "foo" object is an instance of two
different "classes". In fact, foo is an "instance" of Foo.prototype
(i.e., has it as a prototype), no matter where you store that
object. Inheritance has nothing to do with the Foo and Bar functions,
they just facilitate the creation of objects.
function Circle(radius) {
this.r = radius;
}

This is a constructor function. It is used to create objects with a
common (empty) prototype (Circle.prototype).

And Class variable/function can be done:
Circle.PI = 3.14159;

That should correspond to Java's static fields in that they are
properties of the "class".

function Circle_max(a,b) { ... }
Circle.max = Circle_max;

And a static/class method.

So far the metaphor holds.

And instance method is a little bit cumbersome:

function Circle_area() { ... };
Circle.prototype.area = Circle_area;

There is not a huge difference between that and
function Circle() {
...
this.area = Circle_area;
}
when you think in class-based patterns. It is only in the prototype
based view that there is a difference between a function in the object
itself and in the prototype.

To instanize a circle, just do,

var c = Circle(23.0);

Yes, that creates an object. You can think of Circle as its class, but
that's all in your head (and it can be a useful way to think too). But
in the language, there is no class.

So it looks not clear that what the class of an initialized object is.
i.e. var obj = { a:1, b:2, f:function () {...}, .... }

There is no class. The object is an "instanceof" the constructor
function Object, but all (non-host) objects are. Its "constructor"
property also points to the Object function.

Thinking in prototypes, it is simple what happens: The new object
is created. Its prototype is the original value of Object.prototype.
That's the entire story.
More func with "inheritance":
---
function Foo(){}
var foo = new Foo();

function Bar(){}
Bar.prototype = foo;
var bar = new Bar();

function Baz(){}
Baz.prototype=bar;
var baz = new Baz();

Bar.prototype = new Object();
alert([baz instanceof Baz, baz instanceof Bar, baz instanceof Foo);
---
Guess the result :)

Lasse Reichstein Nielsen wrote:

Please don't top post.
/L

Jul 20 '05 #6

P: n/a
> So the answer of my question should be,

Object obj = { .... } is and only is an instance of Object.

Is it a correct statement?


Syntactically, no. This isn't Java. There is no form like

Object obj = ...

I don't use instanceof. I don't trust it. I do make very heavy use of
constructor functions to create objects, and prototypes to reduce memory
usage, and augmentation to specialize objects.

Class hierarchies can be very shallow because you don't need to make a
new class to each minor variation.

Even in static class-based languages like Java, I think it is bad form
to have to routinely cast or query objects about their types. But I am
in the minority on that.

http://www.crockford.com/javascript/private.html
Jul 20 '05 #7

P: n/a
Nick <nb**********@hotmail.com> writes:
One correction:

function Foo(){}
function Bar(){};
var foo = new Foo();
var bar = new Bar();
Bar.prototype=Foo.prototype;
alert([foo instanceof Foo,
foo instanceof Bar,
bar instanceof Foo,
bar instanceof Bar]);
---
shows: true, true, false, true
instead of: true,true,false, false
You are using Mozilla? I only checked the code in Opera, but checking
now, it seems that IE agrees with me, and Mozilla with you.

Reading the ECMAScript standard, I'd say that Mozilla is in violation.
So the answer of my question should be,

Object obj = { .... } is and only is an instance of Object.

Is it a correct statement?


Almost (i.e., in most cases yes). The object "obj" has as its prototype
the original value of Object.prototype. That makes it an instance of
Object (and that can't be changed, since the prototype property of Object
is read-only and don't-delete).

It is also an instance of any other function with the same prototype:

var x = {x:42};
function Foo(){};
Foo.prototype = Object.prototype;
alert(x instanceof Foo);

/L 'please don't top post'
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #8

P: n/a
Lasse Reichstein Nielsen wrote:
Nick <nb**********@hotmail.com> writes:

One correction:

function Foo(){}
function Bar(){};
var foo = new Foo();
var bar = new Bar();
Bar.prototype=Foo.prototype;
alert([foo instanceof Foo,
foo instanceof Bar,
bar instanceof Foo,
bar instanceof Bar]);
---
shows: true, true, false, true
instead of: true,true,false, false

Yes, I am using Mozilla. As Douglas (in the thread) said, instanceof is
not trustable. And it really scares me away from using the advance OO
features of Javascript since even Mozilla can implement it differently.
That's too bad - although most of the tasks can be done easily without
the OO approach.


You are using Mozilla? I only checked the code in Opera, but checking
now, it seems that IE agrees with me, and Mozilla with you.

Reading the ECMAScript standard, I'd say that Mozilla is in violation.

So the answer of my question should be,

Object obj = { .... } is and only is an instance of Object.

Is it a correct statement?

Almost (i.e., in most cases yes). The object "obj" has as its prototype
the original value of Object.prototype. That makes it an instance of
Object (and that can't be changed, since the prototype property of Object
is read-only and don't-delete).

It is also an instance of any other function with the same prototype:

var x = {x:42};
function Foo(){};
Foo.prototype = Object.prototype;
alert(x instanceof Foo);

/L 'please don't top post'


Jul 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.