473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to make a Class method for initialed obj?

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
8 1551
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
> 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
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
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
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Hitesh Patel | last post by:
hi friends, read following code and my problem. class B { private: public: void fun1(void) {
10
by: Nicolas Fleury | last post by:
Hi everyone, I was wondering if it would make sense to make staticmethod objects callable, so that the following code would work: class A: @staticmethod def foo(): pass bar = foo() I...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
28
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
1
by: ramiatia | last post by:
Hi all, I'm writing a Win32 app with VC 2005 unmannaged code. I started a new project from scratched. I want to have a class for the Dialog form that handle everyting for that form (processing...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.