473,320 Members | 2,177 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,320 software developers and data experts.

var Something= new Something() What does it mean ?

Hi,

I was "studying" the famous (public code) BusyBox. I see the
instruction:
var busyBox = new BusyBox

as in

var busyBox = new BusyBox("BusyBox1", "busyBox", 4, "gears_ani_",
".gif", 125, 147, 206)
What that syntax "var busyBox = new ... " actually means ?? What is
NEW. I use all the time constructors in .NET, but I am unsure about
Javascript. So far I have never seen it in javascript.

Does it mean I can create an object with properties, fields, methods,
etc ?

-P

Oct 5 '06 #1
9 3070
VK

pa***********@libero.it wrote:
What that syntax "var busyBox = new ... " actually means ?? What is
NEW.
new ConstructorName() means "make me a new instance of this kind of
object and return a reference to this instance".

<script type="text/javascript">

function Dell() {
this.producer = 'Dell, Inc.';
this.URL = 'http://www.dell.com';
}

function OptiFlex (processor, memory) {
Dell.call(this);
this.processor = processor || 'Athlon 1.2GHz';
this.memory = memory || '128Mb';
}

var myComputer = new OptiFlex();
alert(myComputer.producer);
alert(myComputer.processor);
alert(myComputer instanceof OptiFlex);
</script>

Oct 5 '06 #2

VK ha scritto:
pa***********@libero.it wrote:
What that syntax "var busyBox = new ... " actually means ?? What is
NEW.

new ConstructorName() means "make me a new instance of this kind of
object and return a reference to this instance".

This is really nice. I didn't get that it is possible to "create"
objects in javascript. Beautiful!

I see you defined fields. Is it possible to define also "methods" and
"properties" like in .net?
And don't tell me we also have inheritance or interfaces! Do we?

-P
>
<script type="text/javascript">

function Dell() {
this.producer = 'Dell, Inc.';
this.URL = 'http://www.dell.com';
}

function OptiFlex (processor, memory) {
Dell.call(this);
this.processor = processor || 'Athlon 1.2GHz';
this.memory = memory || '128Mb';
}

var myComputer = new OptiFlex();
alert(myComputer.producer);
alert(myComputer.processor);
alert(myComputer instanceof OptiFlex);
</script>
Oct 5 '06 #3
VK
pamelaflue...@libero.it wrote:
I see you defined fields. Is it possible to define also "methods" and
"properties" like in .net?
in .Net what? ;-) There is no such language, that's an interface atop
of a set of languages - including JScript. You must be working with C#,
and yes, fields (simple properties) and methods are welcome. You only
cannot define compound properties with getter and setter (simply
speaking something called as property but acting like method() )

function Dell() {
this.producer = 'Dell, Inc.';
this.URL = 'http://www.dell.com';
}

function OptiFlex (processor, memory) {
Dell.call(this);
this.processor = processor || 'Athlon 1.2GHz';
this.memory = memory || '128Mb';
this.showConfig = function() {
alert(this.processor + ' ' + this.memory);
}
}

var myComputer = new OptiFlex();
alert(myComputer.producer);
alert(myComputer.processor);
alert(myComputer instanceof OptiFlex);
myComputer.showConfig();
And don't tell me we also have inheritance or interfaces!
Sorry sis, no core implemented interfaces yet :-( :-)

And with inheritance it is complicated matter, because natively
JavaScript/JScript implements prototype-based (not class-based)
inheritance. It is flexible enough to be whatever you want it to be, so
class inheritance can be very well emulated. In my sample I used the
"classy" approach as I felt that would be the most pleasing for your
eyes :-)

Yet it is not a real hard-coded inheritance like in C-languages. Say
OptiFlex() and Dell() constructor are not in "super - extends"
relations, these are just two independent functions fith the same core
constructor. A production chain is being made only at the moment of
instance creation and it disappear right after (try study
OptiFlex.constructor and Dell.constructor)

This emulation can be fully sufficient though for your tasks. If not,
study the real mechanics of the prototype inheritance (look at
..prototype property and its manipulations).

Oct 5 '06 #4

VK ha scritto:
pamelaflue...@libero.it wrote:
I see you defined fields. Is it possible to define also "methods" and
"properties" like in .net?

in .Net what? ;-) There is no such language, that's an interface atop
of a set of languages - including JScript.
:)) to me they look all the same ;)

You must be working with C#,
and yes, fields (simple properties) and methods are welcome. You only
cannot define compound properties with getter and setter (simply
speaking something called as property but acting like method() )
Actually, it seems to me that not many people are organizing
javascript
into "objects". Is this a right impression? Or just didn't see enough
code ?

Also it's probably a little confusing (to me) that the object
definition "looks like" a function.
And don't tell me we also have inheritance or interfaces!

Sorry sis, no core implemented interfaces yet :-( :-)

And with inheritance it is complicated matter, because natively
JavaScript/JScript implements prototype-based (not class-based)
inheritance. It is flexible enough to be whatever you want it to be, so
class inheritance can be very well emulated. In my sample I used the
"classy" approach as I felt that would be the most pleasing for your
eyes :-)

Yet it is not a real hard-coded inheritance like in C-languages. Say
OptiFlex() and Dell() constructor are not in "super - extends"
relations, these are just two independent functions fith the same core
constructor. A production chain is being made only at the moment of
instance creation and it disappear right after (try study
OptiFlex.constructor and Dell.constructor)

This emulation can be fully sufficient though for your tasks. If not,
study the real mechanics of the prototype inheritance (look at
.prototype property and its manipulations).
I need to digest these concepts, to see the difference with the
concepts I am used to ...

Thanks for the nice examples...

-P

Oct 5 '06 #5
pa***********@libero.it wrote:
>
Actually, it seems to me that not many people are organizing
javascript
into "objects". Is this a right impression? Or just didn't see enough
code ?
Probably the latter.
Also it's probably a little confusing (to me) that the object
definition "looks like" a function.
It is. JavaScript functions are objects.
--
Ian Collins.
Oct 5 '06 #6
VK

pa***********@libero.it wrote:
:)) to me they look all the same ;)
Aha, ".Net rulez", I know... :-)
Actually, it seems to me that not many people are organizing
javascript into "objects". Is this a right impression?
More like an allusion :-) The trick is of *how* do they organize their
objects. One of powers of JavaScript is that it was made a bit like
Esperanto, so everyone would find a similarity to scream "Hey, it's
just like in my language!" Some people see just a classy inheritance
(with some limitations to work around) and just stay in their. That
maybe helpful if you are using javascript in conjuction with another
C-like language and you don't want to switch the "programming pattern"
in your mind every single minute. I'm the one of this kind very often.
You may look at prototype.js library (google for "prototype.js") for a
sample of classy emulation in a prototype-based language.
Also it's probably a little confusing (to me) that the object
definition "looks like" a function.
In javascript the call context defines everything. If you call a
function as constructor it acts like consructor, if as a function - it
acts as a function. "Do what's you want but do don't fling me in dat
brier-patch" :-)
I need to digest these concepts, to see the difference with the
concepts I am used to ...
I cannot sampling prototype inheritance at 3am :-) but if you search
this newsgroup for "prototype inheritance" you'll find something useful
for sure. And there are real gurus here to explain all details.

Oct 5 '06 #7
pa***********@libero.it wrote:
VK ha scritto:
>pa***********@libero.it wrote:
>>What that syntax "var busyBox = new ... " actually means ?? What is
NEW.

new ConstructorName() means "make me a new instance of this
kind of object and return a reference to this instance".
It would be more accurate to say that his creates a new instance of a
javascript object and then performs a number of (implied or direct)
actions upon that object. The javascript object is dynamic and can have
properties added to it at any time. Constructor functions are just a
mechanism for having like changes applied to new Objects, so that the
resulting objects can be regarded as being of some 'type' or 'class'.
This is really nice. I didn't get that it is possible to "create"
objects in javascript. Beautiful!
It is actually almost impossible not to create objects in javascript.
Presumably you mean objects of some programmer defined 'type' or
'class'.
I see you defined fields. Is it possible to define also "methods" and
"properties" like in .net?
Methods are just references to functions assigned to properties of
objects _and_ called as through property accessors (as the - this -
value in a function is determined _only_ by how the function is called.
That is, there is no inherent relationship between a function that is
used as a method and any object instance that is using it.).
And don't tell me we also have inheritance or interfaces! Do we?
<snip>

Native inheritance is through the prototype chain. An object instance
is made into the prototype of any new object, usually by assigning a
reference to that object to the - prototype - property of the
constructor (before it is use to create new objects) or by modifying
the object that is already the default prototype of the constructor. If
an attempt is made to look up properties on an object and that object
does not have such properties itself its prototype is examined to see
if it has such a property and if it does the value of that property is
used (else the prototype's prototype is examined (if it has one)).

Thus the object on the prototype of a newly created object provide
default values (including default methods) for that object. Once a
different value is written to the object itself the value on objects on
the prototype chain are 'masked' and their values will no longer be
returned if the value of the property is read from the object instance.

The prototype chain cannot branch (it is a chain not a tree) so there
is no equivalent to multiple inheritance in javascript (though the
concept can be implemented once an appreciation of 'classes' in
javascript not being a native characteristic of the language but
instead being a programmer imposed design concept that is manifest in
making similar modifications to instances of the single native object
'class'.

Javascript's prototype inheritance usually resembles something like:-

function Dell() {
;
}
Dell.prototype.producer = 'Dell, Inc.';
Dell.prototype.URL = 'http://www.dell.com';
function OptiFlex (processor, memory) {
if(processor){
this.processor = processor;
}
if(memory){
this.memory = memory;
}
}
OptiFlex.prototype = new Dell();
OptiFlex.prototype.processor = 'Athlon 1.2GHz';
OptiFlex.prototype.memory = '128Mb';
As there are no 'classes' in javascript and all objects are the
actually the same type of object, just modified in differing ways, the
sense in which an interface is defined is only multiple objects having
the sharing a sub-set of defined methods and properties. It is trivial
to implement that.

Because the objects in javascript are dynamic, in addition to defining
the like-sub-set of properties and methods that might be called an
interface on groups of 'classes', and interface may be added to an
object. For example, suppose an interface known as "X" defined - get -,
- put - and - size - methods, a factory function could be used that
would take an object of any 'class' as its argument and add the
interface to the object:-

function augmentWithX_Interface(obj){
var storage = [];
obj.get = function(key){
...
};
obj.put = function(key, value){
...
};
obj.size = fucntion(){
...
};
return obj;
}

var dellWithX = augmentWithX_Interface( new Dell() );

(Note: this example is closure-based in its mechanism so it is
something you will not want to even consider using until you fully
understand prototype inheritance).

Richard.

Oct 5 '06 #8

pa***********@libero.it wrote:

[snip]
Actually, it seems to me that not many people are organizing
javascript
into "objects". Is this a right impression? Or just didn't see enough
code ?
Hi

If you search this news group or on a web search engine, you will find
a vast amount of writing and web sites on the subject of object
oriented style programming in Javascript.

Also have a look at a good book:<URL : http://www.davidflanagan.com/>
Also it's probably a little confusing (to me) that the object
definition "looks like" a function.
In rough terms, a Function in Javascript can act as a function, a
constructor for your own objects, and a method of those objects. The
"this" keyword is central to its function as a constructor and a
method, and your research should include understanding "this"
thoroughly.

Regards

Julian Turner

Oct 5 '06 #9

Julian Turner ha scritto:
pa***********@libero.it wrote:

[snip]
Actually, it seems to me that not many people are organizing
javascript
into "objects". Is this a right impression? Or just didn't see enough
code ?

Hi

If you search this news group or on a web search engine, you will find
a vast amount of writing and web sites on the subject of object
oriented style programming in Javascript.

Also have a look at a good book:<URL : http://www.davidflanagan.com/>

Thanks you very much for all the nice inputs. (I doubt any book can
compete with you guys!! : ) )
-P
>
Also it's probably a little confusing (to me) that the object
definition "looks like" a function.

In rough terms, a Function in Javascript can act as a function, a
constructor for your own objects, and a method of those objects. The
"this" keyword is central to its function as a constructor and a
method, and your research should include understanding "this"
thoroughly.

Regards

Julian Turner
Oct 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Luka Milkovic | last post by:
Hello, I have a little problem and although it's little it's extremely difficult for me to describe it, but I'll try. I have written a program which extracts certain portions of my received...
56
by: maadhuu | last post by:
hello, this is a piece of code ,which is giving an error. #include<stdio.h> int main() { int a =10; void *p = &a; printf("%d ", *p ); //error....why should it //be an error ?can't the...
11
by: BoloBaby | last post by:
OK, check this out... I have a form with a panel control and button on it (outside the panel control). I have two event handlers - one handles the click event of the button on the form. The...
7
by: Daniel Rudy | last post by:
Hello, I have a peice of code that I'm making an attempt to code. The problem is that I need to return an arbitrary number of char strings. int function(char *fname, int *dcount, char *data)...
5
by: Donkey | last post by:
Hello, I want to find out how many digits does each date type have and how many bytes does it occupy in memory, so I wrote a program below: #include <stdio.h> const long double...
13
by: Snis Pilbor | last post by:
Hello, Here is an idea I've been toying with to speed up programs but still keep them portable. It's just a very handwavey rough description right now since I haven't worked out details. The...
59
by: MotoK | last post by:
Hi Experts, I've just joined this group and want to know something: Is there something similar to smart pointers in C or something to prevent memory leakages in C programs. Regards MotoK
25
by: Jon Slaughter | last post by:
I have some code that loads up some php/html files and does a few things to them and ultimately returns an html file with some php code in it. I then pass that file onto the user by using echo. Of...
1
by: jasonwthompson | last post by:
I'm working on some code and I noticed that there are functions that contain a $ in them. Does anyone know what they mean? I suspect that they are macro definitions, but I'm not sure if the $ is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.