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

P

pamelafluente

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
 
V

VK

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>
 
P

pamelafluente

VK ha scritto:
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
 
V

VK

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).
 
P

pamelafluente

VK ha scritto:
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.
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
 
I

Ian Collins

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.
 
V

VK

:)) 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.
 
R

Richard Cornford

VK ha scritto:

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.
 
J

Julian Turner

(e-mail address removed) 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: said:
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
 
P

pamelafluente

Julian Turner ha scritto:
(e-mail address removed) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top