Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advanced Object Questions 1

Status
Not open for further replies.

UNIMENT

Programmer
Feb 15, 2002
302
0
0
US
Question 1:
Does anybody know how to make a constructor function that creates objects that can be accessed like an Array object?
like:
Code:
function specialObject() {
[What do I put here?]
}
var myArray=new specialObject('my','name','is','whatever');
alert(myArray[1]);
And have an alert box pop up saying "name"


Question 2:
Is it possible to have a variable pointing specifically to an object's property?
Example, if you set the variable top to point to myDiv.style.pixelTop:
Code:
top=3;
alert(myDiv.style.top)
And it alerts "3px".


Question 3:
Javascript properties. No, not the normal properties, but more similar to the built-in properties. For instance, in IE, when you set a Div's
Code:
style.pixelTop
to a number, its
Code:
style.top
also changes and its position changes. This points to the idea that it calls a function when you change a property through the = operator. Is it possible in normal Javascript to call a function when changing object properties? It would be very useful.


Question 4:
Code:
typeof
function. Is it possible to create functions like it?
Keep in mind that the syntax to call typeof is:
Code:
typeof [variable]
This means that it is called using syntax other than is normal functions. Making functions like that would be equally useful.


Question 5:
Code:
in
. Why does the browser give me errors when I say
Code:
in
? Example:
Code:
var in='hey there';
The error is:
Code:
Error: Expected Identifier
Kind of strange...


Thanks for helping.
 
Q1:
To define your own object, use this:

Constructor:[tt]
function car(maker, model, year) {
//define object's properties here
  this.maker = make
  this.model = model
  this.year = year
}[/tt]

then you instantiate an object:
[tt]someCar = new car("Ford", "super-XZ-999", 2007)[/tt]

you may access it's fields also:
[tt]someCar.model[/tt]

Also, you can add a new property to already defined object:
[tt]car2.color = "black"[/tt]
Of course, it will be added only to [tt]car1[/tt] object, and the others will stay the same.

I'm sure that you'll be able to access all objects as car[] array, but I didn't tried it at the moment.


Q2:
In general, yes. But not always: some properties are read-only, and some cannot be changed dynamically.

Q3:
It's not so clear what is it about.
If you talk about properties of objects that you create by javascript, all this should be done yourself by defining some function in object constructor.

Q4:
I never thought about it. What it can be useful for?

Q5:
in is javascript-reserved word.
It is used in [tt]for ... in[/tt] loops.
Here are few examples:
and

Hope it will help you.
good luck
 
Thanks... I had forgotten about for...in loops.




I guess I didn't make myself clear enough about what I meant in the rest of the questions, though. I know how to make objects and their constructors. I just wonder how you can access the objects as arrays -- how to make the objects respond just as if they were Array objects.
Code:
var myObject={ /* Whatever's inside... */ };
alert(myObject[0]);


It would be nice to have pointers directly to "normal" variables -- Strings and Arrays, etc. This would allow you to create custom objects that can be accessed and respond the same way in all browsers but deep down inside, act differently in the different browsers.
For instance, say we had a DIV and we had an object pointing to it. We want to have the pointer directly access the DIV's style object and yet still access the DIV itself:

Code:
var myDiv=document.all.divver; // myDiv points to the DIV
var myDivObject= new Object; // equals whatever...
myDivObject.top= ; // Access myDiv's style through the user-created object, myDivObject
myDivObject.innerHTML= ; // Access myDiv itself through myDivObject
Code like the above would require a pointer directly to the DIV's child, .innerHTML, and a pointer directly to the DIV's style child, .top. Being capable of doing this would enable the creation of objects that are very easy to access no matter the browser, thus creating the most maintainable javascript on the planet ;-).



Being able to call functions when using the = operator would be useful because of the simplicity in syntax. Would you rather set the position of a DIV by putting
Code:
myDiv.setTop( /*coordinate*/ );
Or would you rather put:
Code:
myDiv.top = ;// coordinate...

... The latter is simpler, easier to type, easier to read, and more understandable. The latter is by far preferrable.



The [code]typeof
function is also an example of strange syntax that would make life easier. Rather than putting an equal sign or parentheses, you could just put a space between the function and its parameter. This would also be quite useful in the same manner.




Is it possible in JavaScript to reproduce the same syntax usage as described above?
 
Strange... There appears to be a bug in our server's message parser. The last message that I posted, near the end where it says:

Code:
The
typeofy function // ...

It was supposed to say:

The typeof function // ...

Except with "typeof" within [ code ][ /code ] tags...
Strange...
 
waiting for other peoples' comments...
 
In order to make your custom object appear like an array, assign a toString function like:
Code:
    function car ( make, model, year ) {
        this.make  = make;
        this.model = model;
        this.year  = year;
    }

    car.prototype.toString = function () {
        return "Make  : " + this.make +
               "\nModel : " + this.model +
               "\nYear  : " + this.year;
    }

    someCar = new car( "Ford", "super-XZ-999", 2007 );

    alert( someCar );
Cheers, Neil :cool:
 
Sorry, to make it appear like an array, the toString function should simply be:
Code:
    car.prototype.toString = function () {
        return this.make  + "," +
               this.model + "," +
               this.year;
    }
Cheers, Neil ;-)
 
Question 1:

There is no way to achieve this in standard Javascript. There may be some new support for this in JScript.NET, though I haven't had a chance to look deeply into it.

Question 3:

No, but I wish.

Question 4:

No, but I wish.

All of these 'I wishes' are available in C#, though =). jared@eae.net -
 
Uniment, relating to your first Q:

Q1: Answer


<script>

function car(company, modelType, yearProd) {
this.maker = company;
this.model = modelType;
this.year = yearProd;
}

someCar = new car(&quot;Ford&quot;, &quot;super-XZ-999&quot;, 2007);
var myArray = new Array();
myArray[0] = someCar;

alert(myArray[0].maker)
</script>
 
I may have been mistaken, is this what you want?

function Uniment()
{
var arg,arglen,i;

arg = arguments;
arglen = arg.length;

for(i=0;i<arglen;i++)
{
this = arg;
}
}

uni = new Uniment('d')
un2 = new Uniment('x','d')

alert(uni[0]) // should display 'd'
alert(un2[0]) // should display 'x'
alert(un2[1]) // should display 'd' jared@eae.net -
 
function Uniment()
{
var arg,arglen,i;

arg = arguments;
arglen = arg.length;

for(i=0;i<arglen;i++)
{
this = arg;
}
}

uni = new Uniment('d')
un2 = new Uniment('x','d')

alert(uni[0]) // should display 'd'
alert(un2[0]) // should display 'x'
alert(un2[1]) // should display 'd' jared@eae.net -
 
Jaredn: Yes! That's exactly what I wanted! thanks!
 
ok... Here's a little followup:

You can declare single objects like this:
Code:
myObject={
 0:&quot;Array Element 0&quot;,
 1:&quot;Array Element 1&quot;
}

Next step... I'd like to know if it's possible to make it even more like an
Code:
Array
. For example, when you change the
Code:
length
property of an
Code:
Array
object, it will create/destroy elements (Even though you used the &quot;=&quot; operator). And, if you create elements in the
Code:
Array
, the
Code:
length
property will grow. Any ideas on how to do something like this?
 
You cannot change the functioning of the '=' operator in standard javascript. Supposedly, future versions of javascript will have support for Property setting (similar to that found in C#). You would actually have to create methods that set/get the length property and perform the desired manipulations upon invoking them. jared@eae.net -
 
damn!! Not what I wanted to hear :-\
bluebrain.gif
blueuniment.gif
 
I've found that Netscape 4+ supports the
Code:
watch()
and
Code:
unwatch()
methods for executing functions when object properties are changed, and for ceasing to.

Here's the syntax:
Code:
function myFunction() {}
myObject.watch(&quot;myProperty&quot;,myFunction);
myObject.unwatch(&quot;myProperty&quot;);
&quot;myProperty&quot; is a string specifying the name of the property to keep an eye out for. As you could expect, the
Code:
watch()
function sets the browser to listen for changes, and the
Code:
unwatch()
function does the opposite.


Unfortunately, through quite a bit of research, I have not found any IE equivalent.


Ahhh.... Wouldn't it be nice if they just used normal events, such as
Code:
onchange
. If they had, life would be sooo much easier (for cross-browser scripting libraries). :-\
bluebrain.gif
blueuniment.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top