L
Luke
Here is my emails to Danny Goodman (but probably he is very busy so he
didn't answered it).
First email(simple): Subject: JavaScript Arrays
"
We all know the array can act like HashMap, but is there a shortcut
for creating such array ? eg
//simple array indexed via numbers
var a = new Array("a", "fdsf", "sada");
or,
a["a", "fdsf", "sada"];
, bo how a to make it an HashMap ? maybe that:
a["key1":"a", "key2":"fdsf", "key3":"sada"]
As last we all know that it works for objects:
a = { "key1":"a", "key2":"fdsf", "key3":"sada"};
Thanks for answer;
Lukasz(Luke) Matuszewski
"
Second email: Subject: JavaScript advanced elements
"
Hi !
I am a student from Tech. Univ. Of Gdansk and I am sort of advanced
begginer in JavaScript and ECMAScript standards. When I was reading
many scripts on different general projects like js calendar or js
menu-realted projects i found many things that i think to understand
but i want to know more about it with also realtion to ECMAScript
standard. Here is the following things:
1. Moder-browsers quick object construction like this:
function DynamicTree(id) {
...
this.img = {
"branch": "tree-branch.gif",
"doc": "tree-doc.gif",
"folder": "tree-folder.gif",
"folderOpen": "tree-folder-open.gif",
"leaf": "tree-leaf.gif",
"leafEnd": "tree-leaf-end.gif",
"node": "tree-node.gif",
"nodeEnd": "tree-node-end.gif",
"nodeOpen": "tree-node-open.gif",
"nodeOpenEnd": "tree-node-open-end.gif" };
...
this.init = function() {
var p, img;
for (p in this.img) {
this.img[p] = this.path + this.img[p];
}
for (p in this.img) {
this.imgObjects.push(new Image());
this.imgObjects.getLast().src = this.img[p];
this.img[p] = this.imgObjects.getLast().src;
}
this.parse(document.getElementById(this.id).childNodes,
this.tree, 1);
this.loadState();
if (window.addEventListener) {
window.addEventListener("unload", function(e) { self.saveState(); },
false); }
else if (window.attachEvent) { window.attachEvent("onunload",
function(e) { self.saveState(); }); }
this.updateHtml();
};
...
this.nodeClick = function(id) {
var el = document.getElementById(id+"-section");
var node = document.getElementById(id+"-node");
var icon = document.getElementById(id+"-icon");
if (el.style.display == "block") {
el.style.display = "none";
if (this.allNodes[id].isLast()) { node.src =
this.img.nodeEnd; }
else { node.src = this.img.node; }
icon.src = this.img.folder;
this.opened.removeByValue(id);
} else {
el.style.display = "block";
if (this.allNodes[id].isLast()) { node.src =
this.img.nodeOpenEnd; }
else { node.src = this.img.nodeOpen; }
icon.src = this.img.folderOpen;
this.opened.push(id);
}
/* fix ie bug - images not showing */
if (node.outerHTML) { node.outerHTML = node.outerHTML; }
if (icon.outerHTML) { icon.outerHTML = icon.outerHTML; }
};
...
this.updateHtml = function() {
document.getElementById(this.id).innerHTML = this.toHtml();
};
}
Here is my question: we see that this.img is an object, but its
properties are closed in quotes (eg "branch": "tree-branch.gif"). Then
we see in code in function this.init = function() that its properties
are changed via array construction eg:
for (p in this.img) {
this.img[p] = this.path + this.img[p];
}
, but it is not all - when we read the code more we will see that
properties of img object is read via standard way eg:
this.img.nodeEnd;
Furthermore i have seen on other scripts that other author scripters
used this array convention to object/method/property detection
purposes and even for calling a method so it looked like: obj[p](i , j
, o) (where obj was document object and p was "addEventListener"...).
My question is when it is permitted to use this convention and how it
relates to ECMAScript... and most of all how browsers support this
convention and when i can use it ? Especially does i have to put my
properties around double quotes like this:
this.img = {
"branch": "tree-branch.gif",
...
, and can i use this array convention when i create object like this
(whitout double quotes, like presented in your book):
this.img = {
branch: "tree-branch.gif",
...
My last question to this topic is does it have realtion to
ECMAScript[[isPropertyEnumerable]], so when i create object with
properties in quotes it is enumerable in for-in and without double
quote it isnt ?
As my comment for this that it is messy, becouse it is hard to find
does object is an true Array or other object. Another missleading is
that i know when you do:
var a = new Array();
a["src"] = '/my.gif';
and then,
a.src = 'my2.gif';
, then will i have two diffrent value one in a["src"] and one in a.src
(i thin not).
Please help, becouse it is really confusing me.
2. Some things related to js shortcuts:
In constructor like:
function Constr(a , b , c) {
this.a = a || "myVal";
....
}
the notation
this.a = a || "myVal";
is quite natural, becouse it is bitwise OR and when a is not supplied
it is undefined and thus it has no value so this.a will be "myVal".
But i have seen also this:
this._c = this._c | 0;
, where _c was a counter. When _c is first called it is undefined and
the result will be false (this._c will be false). The this _c is used
as a counter like this._c ++; does it make sense ? Should it be like:
this._c = this._c || 0;
, to make values correct ?
3. When i create variable like:
var _c;
, does it have value of null ?
Thanks in advance for answer, waiting for quick reply
Lukasz(Luke) Matuszewski
"
I really would like to ready about this things, espacially to here
about the safest ways to doing OOP in JavaScript...
didn't answered it).
First email(simple): Subject: JavaScript Arrays
"
We all know the array can act like HashMap, but is there a shortcut
for creating such array ? eg
//simple array indexed via numbers
var a = new Array("a", "fdsf", "sada");
or,
a["a", "fdsf", "sada"];
, bo how a to make it an HashMap ? maybe that:
a["key1":"a", "key2":"fdsf", "key3":"sada"]
As last we all know that it works for objects:
a = { "key1":"a", "key2":"fdsf", "key3":"sada"};
Thanks for answer;
Lukasz(Luke) Matuszewski
"
Second email: Subject: JavaScript advanced elements
"
Hi !
I am a student from Tech. Univ. Of Gdansk and I am sort of advanced
begginer in JavaScript and ECMAScript standards. When I was reading
many scripts on different general projects like js calendar or js
menu-realted projects i found many things that i think to understand
but i want to know more about it with also realtion to ECMAScript
standard. Here is the following things:
1. Moder-browsers quick object construction like this:
function DynamicTree(id) {
...
this.img = {
"branch": "tree-branch.gif",
"doc": "tree-doc.gif",
"folder": "tree-folder.gif",
"folderOpen": "tree-folder-open.gif",
"leaf": "tree-leaf.gif",
"leafEnd": "tree-leaf-end.gif",
"node": "tree-node.gif",
"nodeEnd": "tree-node-end.gif",
"nodeOpen": "tree-node-open.gif",
"nodeOpenEnd": "tree-node-open-end.gif" };
...
this.init = function() {
var p, img;
for (p in this.img) {
this.img[p] = this.path + this.img[p];
}
for (p in this.img) {
this.imgObjects.push(new Image());
this.imgObjects.getLast().src = this.img[p];
this.img[p] = this.imgObjects.getLast().src;
}
this.parse(document.getElementById(this.id).childNodes,
this.tree, 1);
this.loadState();
if (window.addEventListener) {
window.addEventListener("unload", function(e) { self.saveState(); },
false); }
else if (window.attachEvent) { window.attachEvent("onunload",
function(e) { self.saveState(); }); }
this.updateHtml();
};
...
this.nodeClick = function(id) {
var el = document.getElementById(id+"-section");
var node = document.getElementById(id+"-node");
var icon = document.getElementById(id+"-icon");
if (el.style.display == "block") {
el.style.display = "none";
if (this.allNodes[id].isLast()) { node.src =
this.img.nodeEnd; }
else { node.src = this.img.node; }
icon.src = this.img.folder;
this.opened.removeByValue(id);
} else {
el.style.display = "block";
if (this.allNodes[id].isLast()) { node.src =
this.img.nodeOpenEnd; }
else { node.src = this.img.nodeOpen; }
icon.src = this.img.folderOpen;
this.opened.push(id);
}
/* fix ie bug - images not showing */
if (node.outerHTML) { node.outerHTML = node.outerHTML; }
if (icon.outerHTML) { icon.outerHTML = icon.outerHTML; }
};
...
this.updateHtml = function() {
document.getElementById(this.id).innerHTML = this.toHtml();
};
}
Here is my question: we see that this.img is an object, but its
properties are closed in quotes (eg "branch": "tree-branch.gif"). Then
we see in code in function this.init = function() that its properties
are changed via array construction eg:
for (p in this.img) {
this.img[p] = this.path + this.img[p];
}
, but it is not all - when we read the code more we will see that
properties of img object is read via standard way eg:
this.img.nodeEnd;
Furthermore i have seen on other scripts that other author scripters
used this array convention to object/method/property detection
purposes and even for calling a method so it looked like: obj[p](i , j
, o) (where obj was document object and p was "addEventListener"...).
My question is when it is permitted to use this convention and how it
relates to ECMAScript... and most of all how browsers support this
convention and when i can use it ? Especially does i have to put my
properties around double quotes like this:
this.img = {
"branch": "tree-branch.gif",
...
, and can i use this array convention when i create object like this
(whitout double quotes, like presented in your book):
this.img = {
branch: "tree-branch.gif",
...
My last question to this topic is does it have realtion to
ECMAScript[[isPropertyEnumerable]], so when i create object with
properties in quotes it is enumerable in for-in and without double
quote it isnt ?
As my comment for this that it is messy, becouse it is hard to find
does object is an true Array or other object. Another missleading is
that i know when you do:
var a = new Array();
a["src"] = '/my.gif';
and then,
a.src = 'my2.gif';
, then will i have two diffrent value one in a["src"] and one in a.src
(i thin not).
Please help, becouse it is really confusing me.
2. Some things related to js shortcuts:
In constructor like:
function Constr(a , b , c) {
this.a = a || "myVal";
....
}
the notation
this.a = a || "myVal";
is quite natural, becouse it is bitwise OR and when a is not supplied
it is undefined and thus it has no value so this.a will be "myVal".
But i have seen also this:
this._c = this._c | 0;
, where _c was a counter. When _c is first called it is undefined and
the result will be false (this._c will be false). The this _c is used
as a counter like this._c ++; does it make sense ? Should it be like:
this._c = this._c || 0;
, to make values correct ?
3. When i create variable like:
var _c;
, does it have value of null ?
Thanks in advance for answer, waiting for quick reply
Lukasz(Luke) Matuszewski
"
I really would like to ready about this things, espacially to here
about the safest ways to doing OOP in JavaScript...