Daz wrote:
Hi everyone. Sorry for the confusing subject, I couldn't think how best
to word it.
What I would like to know, is if there is an equivilant to this code,
in using JSON.
Remove the script tags and eval it. Note that declared functions and
variables will only be available within the eval scope and will cease
to be available after eval has finished.
>
<script type="text/javascript">
function makeTable(number_of_rows) {
var table = document.createElement('table');
table.border = 1;
number_of_rows = (number_of_rows < 0) ? 0 :
number_of_rows;
Indent posted code using 2 or 4 spaces, manually wrap code at about 70
characters to prevent auto-wrapping.
number_of_rows = (number_of_rows 100) ? 100 :
number_of_rows;
for (var i = 0; i < number_of_rows; i++)
{
table.appendChild(new tableRow);
It would make more sense at this point to do something like:
var row = table.insertRow(-1);
for (var j=0; j<number_of_cells; j++){
var cell = row.insertCell(j); // or row.insertCell(-1)
/* add content to cell */
}
It doesn't make sense to create a new function, however if this is just
for learning then proceed.
[...]
</script>
Note that the makeTable constructer is calling another constructer
They are not "constructors" in the classic OO sense, they are plain
function objects.
(tableRow) several times, and making a totally new row each time. Would
it also be possible for me to call upon the constructer for tableRow
from outside of the makeTable object? For example:
var newRow = new makeTable.tableRow();
Not the way you have written it. You need to add tableRow as a
property of makeTable in the scope that you wish to call it. Declaring
it inside makeTable makes it available only from within makeTable
unless you do something to make it available elsewhere.
Consider:
function foo(){
function bar(){
alert('bar');
}
window.bar = bar;
}
alert('bar: ' + typeof bar);
foo();
alert('bar: ' + typeof bar);
The function object bar is created inside foo as soon as the code is
parsed by the javascript engine. However, it is not assigned to the
value of window.bar until the function foo is executed so the first
alert shows "bar: undefined" and the second "bar: function".
How would I be able to do this? I have read endless tutorials, but I
can't seem to find the answer. Perhaps the answer is obvious, and I
just haven't realised?
There are a number of methods, which is "best" depends on the
situation. If this is for a utility function, the simplest way is to
create a namespace object then add everything as a property of that
object:
var tableUtilities = {};
tableUtilities.makeTable = function(param1, param2)
{
/* function body */
alert('makeTable: ' + param1 + ' ' + param2);
}
tableUtilities.makeTable.makeRow = function(param1, param2)
{
/* function body */
alert('makeTable.makeRow: ' + param1 + ' ' + param2);
}
tableUtilities.makeTable('firstParam', 'secondParam');
tableUtilities.makeTable.makeRow('firstParam', 'secondParam');
Of course in this context, it doesn't make much sense for makeRow to be
a property of makeTable. You may like to read the following articles:
Douglas Crockford - Private Members in JavaScript
<URL:
http://www.crockford.com/javascript/private.html >
Richard Cornford - Private Static Members in Javascript
<URL:
http://www.litotes.demon.co.uk/js_in...te_static.html >
--
Rob