473,796 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("BusyBo x1", "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 3102
VK

pa***********@l ibero.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(myCompute r.producer);
alert(myCompute r.processor);
alert(myCompute r instanceof OptiFlex);
</script>

Oct 5 '06 #2

VK ha scritto:
pa***********@l ibero.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(myCompute r.producer);
alert(myCompute r.processor);
alert(myCompute r instanceof OptiFlex);
</script>
Oct 5 '06 #3
VK
pamelaflue...@l ibero.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.proc essor + ' ' + this.memory);
}
}

var myComputer = new OptiFlex();
alert(myCompute r.producer);
alert(myCompute r.processor);
alert(myCompute r instanceof OptiFlex);
myComputer.show Config();
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.constr uctor and Dell.constructo r)

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...@l ibero.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.constr uctor and Dell.constructo r)

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***********@l ibero.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***********@l ibero.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 "programmin g 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***********@l ibero.it wrote:
VK ha scritto:
>pa***********@l ibero.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.protot ype = new Dell();
OptiFlex.protot ype.processor = 'Athlon 1.2GHz';
OptiFlex.protot ype.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_In terface(obj){
var storage = [];
obj.get = function(key){
...
};
obj.put = function(key, value){
...
};
obj.size = fucntion(){
...
};
return obj;
}

var dellWithX = augmentWithX_In terface( 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***********@l ibero.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.davidflanag an.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***********@l ibero.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.davidflanag an.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
2160
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 e-mail. The content of the e-mail is actually predictable, it has one very long list of numbers, something looking like this:
56
3368
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 compiler make out because //of %d that the pointer is supposed to refer to an integer ?? or is explicit type casting required ??
11
1902
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 other handles a custom "CardInserted" event for a class I wrote that watches for smart cards to be inserted into an attached smart card reader.
7
1655
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) Would this work? If so, then how to you load the data into data? Malloc?
5
2407
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 num=1123222.232121342; main() { Printf("the number occupies: %i, and it is 1123222.232121342 \n",sizeof num); printf("char: %i ,the number is %c\n",sizeof(char),(char)num);
13
2263
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 program would contain lots of little utility functions which do small amounts of work. None of these would actually be run. Rather, they would be typecast as strings and those strings would be examined by the program. In otherwords, the...
59
5147
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
3140
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 course then the file doesn't get seen by the user. Is there any command that essentially executes the code and then echo's it? something that will take a string like '<body>blah<?php echo 'Hello'; ?></body>' and actually interpret the php
1
1284
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 legal in a macro def. Or does a $ have an actual meaning in C/C++?
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10456
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10174
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10012
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7548
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.