Is it possible to instance a function in a JSON object?

K

Kashgarinn

Hi there. I'm a beginner with Javascript, and was wondering if you
perhaps knew this.

I'm creating a plugin for a wiki, and I'd like to instance the
function I'm modifying, instead of taking it completely over, so that
if the authors of the wiki change anything, what I add won't touch
that.

However, they keep the function in a JSON object

Here's an excerpt of the JSON object:
=======
config.formatters = [
{
name: "table",
match: "^\\|(?:[^\\n]*)\\|(?:[fhck]?)$",
lookaheadRegExp: /^\|([^\n]*)\|([fhck]?)$/mg,
rowTermRegExp: /(\|(?:[fhck]?)$\n?)/mg,
cellRegExp: /(?:\|([^\n\|]*)\|)|(\|[fhck]?$\n?)/mg,
cellTermRegExp: /((?:\x20*)\|)/mg,
rowTypes: {"c":"caption", "h":"thead", "":"tbody", "f":"tfoot"},

handler: function(w)
{
/* some code here */
},
rowHandler: function(w,e,prevColumns)
{
/* some code here */
}
},
{ // next function here
},
];
========================

Now, I'd like to add variables and functions to the table object.. So
I was thinking, I could probably instance the table function, I dug up
the information, and I think that this should work:
==============================================
config.macros.tablePlugin = {}
config.macros.tablePlugin.handler = function(place, macroName, params,
wikifier, paramString, tiddler)
{
/* Code goes here */
}
config.macros.tablePlugin = new config.formatters.table;
==============================================

But.. it isn't working.. and I'm suspecting it's because it's in a
JSON object, as I get the error: "Error: TypeError:
config.formatters.table is not a constructor"

- Can someone help me? I'm in a bit of a pickle..
 
T

Thomas 'PointedEars' Lahn

Kashgarinn said:
However, they keep the function in a JSON object

You have not understood how JSON works. There is no JSON object. JSON is
simply a compatible Object initializer notation to store object data and to
transmit that (usually over HTTP.) It is *plain text* until evaluated.

http://en.wikipedia.org/wiki/JSON
Here's an excerpt of the JSON object:
=======
config.formatters = [
{
name: "table",
match: "^\\|(?:[^\\n]*)\\|(?:[fhck]?)$",
lookaheadRegExp: /^\|([^\n]*)\|([fhck]?)$/mg,
rowTermRegExp: /(\|(?:[fhck]?)$\n?)/mg,
cellRegExp: /(?:\|([^\n\|]*)\|)|(\|[fhck]?$\n?)/mg,
cellTermRegExp: /((?:\x20*)\|)/mg,
rowTypes: {"c":"caption", "h":"thead", "":"tbody", "f":"tfoot"},

handler: function(w)
{
/* some code here */
},
rowHandler: function(w,e,prevColumns)
{
/* some code here */
}
},
{ // next function here
},
];
========================

If interpreted by an ECMAScript implementation, that would define
config.formatters as being a reference to an Array object that has two
Object objects and, depending on the implementation `undefined' as its
array element properties. The first element, as an object, would have
a `name' attribute with the value "table" (a string of characters).
Now, I'd like to add variables and functions to the table object..

But, table is not an object.
So I was thinking, I could probably instance the table function,

There is no table function.
I dug up the information, and I think that this should work:
==============================================
config.macros.tablePlugin = {}
config.macros.tablePlugin.handler = function(place, macroName, params,
wikifier, paramString, tiddler)
{
/* Code goes here */
}
config.macros.tablePlugin = new config.formatters.table;
==============================================

But.. it isn't working.. and I'm suspecting it's because it's in a
JSON object, as I get the error: "Error: TypeError:
config.formatters.table is not a constructor"

As I said, there is no table function. Therefore, it cannot be used as
a constructor.

AIUI, in order to be able to help you, one would need to have a look at
the object created from this JSON expression. Given the number of JSON
implementations in different languages, that may not even be an
J(ava)Script/ECMAScript object, so it may be off-topic here.


PointedEars
 
K

Kashgarinn

Kashgarinn said:
However, they keep the function in a JSON object

You have not understood how JSON works. There is no JSON object. JSON is
simply a compatible Object initializer notation to store object data and to
transmit that (usually over HTTP.) It is *plain text* until evaluated.

http://en.wikipedia.org/wiki/JSON


Here's an excerpt of the JSON object:
=======
config.formatters = [
{
name: "table",
match: "^\\|(?:[^\\n]*)\\|(?:[fhck]?)$",
lookaheadRegExp: /^\|([^\n]*)\|([fhck]?)$/mg,
rowTermRegExp: /(\|(?:[fhck]?)$\n?)/mg,
cellRegExp: /(?:\|([^\n\|]*)\|)|(\|[fhck]?$\n?)/mg,
cellTermRegExp: /((?:\x20*)\|)/mg,
rowTypes: {"c":"caption", "h":"thead", "":"tbody", "f":"tfoot"},
handler: function(w)
{
/* some code here */
},
rowHandler: function(w,e,prevColumns)
{
/* some code here */
}
},
{ // next function here
},
];
========================

If interpreted by an ECMAScript implementation, that would define
config.formatters as being a reference to an Array object that has two
Object objects and, depending on the implementation `undefined' as its
array element properties. The first element, as an object, would have
a `name' attribute with the value "table" (a string of characters).
Now, I'd like to add variables and functions to the table object..

But, table is not an object.
So I was thinking, I could probably instance the table function,

There is no table function.
I dug up the information, and I think that this should work:
==============================================
config.macros.tablePlugin = {}
config.macros.tablePlugin.handler = function(place, macroName, params,
wikifier, paramString, tiddler)
{
/* Code goes here */
}
config.macros.tablePlugin = new config.formatters.table;
==============================================
But.. it isn't working.. and I'm suspecting it's because it's in a
JSON object, as I get the error: "Error: TypeError:
config.formatters.table is not a constructor"

As I said, there is no table function. Therefore, it cannot be used as
a constructor.

AIUI, in order to be able to help you, one would need to have a look at
the object created from this JSON expression. Given the number of JSON
implementations in different languages, that may not even be an
J(ava)Script/ECMAScript object, so it may be off-topic here.

PointedEars


Thanks for you reply, if it isn't blatantly obvious, I'm really just a
beginner regarding javascript and any information will be very
helpful.

I was thinking that you wouldn't need the info in the table function,
that's why I didn't add it in there, but as you want it ( or that's
what I think you were asking for ), here it is:
=====================================
config.formatters = [
{
name: "table",
match: "^\\|(?:[^\\n]*)\\|(?:[fhck]?)$",
lookaheadRegExp: /^\|([^\n]*)\|([fhck]?)$/mg,
rowTermRegExp: /(\|(?:[fhck]?)$\n?)/mg,
cellRegExp: /(?:\|([^\n\|]*)\|)|(\|[fhck]?$\n?)/mg,
cellTermRegExp: /((?:\x20*)\|)/mg,
rowTypes: {"c":"caption", "h":"thead", "":"tbody", "f":"tfoot"},

handler: function(w)
{
var table = createTiddlyElement(w.output,"table",null,"twtable");
var prevColumns = [];
var currRowType = null;
var rowContainer;
var rowCount = 0;
w.nextMatch = w.matchStart;
this.lookaheadRegExp.lastIndex = w.nextMatch;
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
while(lookaheadMatch && lookaheadMatch.index == w.nextMatch) {
var nextRowType = lookaheadMatch[2];
if(nextRowType == "k") {
table.className = lookaheadMatch[1];
w.nextMatch += lookaheadMatch[0].length+1;
} else {
if(nextRowType != currRowType) {
rowContainer =
createTiddlyElement(table,this.rowTypes[nextRowType]);
currRowType = nextRowType;
}
if(currRowType == "c") {
// Caption
w.nextMatch++;
if(rowContainer != table.firstChild)
table.insertBefore(rowContainer,table.firstChild);
rowContainer.setAttribute("align",rowCount == 0?"top":"bottom");
w.subWikifyTerm(rowContainer,this.rowTermRegExp);
} else {
this.rowHandler(w,createTiddlyElement(rowContainer,"tr",null,
(rowCount&1)?"oddRow":"evenRow"),prevColumns);
rowCount++;
}
}
this.lookaheadRegExp.lastIndex = w.nextMatch;
lookaheadMatch = this.lookaheadRegExp.exec(w.source);
}
},
rowHandler: function(w,e,prevColumns)
{
var col = 0;
var colSpanCount = 1;
var prevCell = null;
this.cellRegExp.lastIndex = w.nextMatch;
var cellMatch = this.cellRegExp.exec(w.source);
while(cellMatch && cellMatch.index == w.nextMatch) {
if(cellMatch[1] == "~") {
// Rowspan
var last = prevColumns[col];
if(last) {
last.rowSpanCount++;
last.element.setAttribute("rowspan",last.rowSpanCount);
last.element.setAttribute("rowSpan",last.rowSpanCount); // Needed
for IE
last.element.valign = "center";
}
w.nextMatch = this.cellRegExp.lastIndex-1;
} else if(cellMatch[1] == ">") {
// Colspan
colSpanCount++;
w.nextMatch = this.cellRegExp.lastIndex-1;
} else if(cellMatch[2]) {
// End of row
if(prevCell && colSpanCount > 1) {
prevCell.setAttribute("colspan",colSpanCount);
prevCell.setAttribute("colSpan",colSpanCount); // Needed for IE
}
w.nextMatch = this.cellRegExp.lastIndex;
break;
} else {
// Cell
w.nextMatch++;
var styles = config.formatterHelpers.inlineCssHelper(w);
var spaceLeft = false;
var chr = w.source.substr(w.nextMatch,1);
while(chr == " ") {
spaceLeft = true;
w.nextMatch++;
chr = w.source.substr(w.nextMatch,1);
}
var cell;
if(chr == "!") {
cell = createTiddlyElement(e,"th");
w.nextMatch++;
} else {
cell = createTiddlyElement(e,"td",null,"cell");
// cell.onmouseover = function() {this.addClass = "active";};
// cell.onmouseout = function() {this.removeClass = "active";};
}
prevCell = cell;
prevColumns[col] = {rowSpanCount:1,element:cell};
if(colSpanCount > 1) {
cell.setAttribute("colspan",colSpanCount);
cell.setAttribute("colSpan",colSpanCount); // Needed for IE
colSpanCount = 1;
}
config.formatterHelpers.applyCssHelper(cell,styles);
w.subWikifyTerm(cell,this.cellTermRegExp);
if(w.matchText.substr(w.matchText.length-2,1) == " ") //
spaceRight
cell.align = spaceLeft ? "center" : "left";
else if(spaceLeft)
cell.align = "right";
w.nextMatch--;
}
col++;
this.cellRegExp.lastIndex = w.nextMatch;
cellMatch = this.cellRegExp.exec(w.source);
}
}
},
===================================

you can also see it on: http://www.pastebin.ca/658200 - just in case
if groups.google.com screws with the code.

This is code from a Wiki which is pure javascript/XHTML, as I'm
learning javascript by trying to edit this, I still haven't understood
all of the things in its code.

If you were asking for the function which activates the
config.formatter container, then here it is:

in themain() function:
============
formatter = new Formatter(config.formatters);
============

the Formatter function:
=============
function Formatter(formatters)
{
this.formatters = [];
var pattern = [];
for(var n=0; n<formatters.length; n++) {
pattern.push("(" + formatters[n].match + ")");
this.formatters.push(formatters[n]);
}
this.formatterRegExp = new RegExp(pattern.join("|"),"mg");
}
============

..... I'm sadly nowhere closer to discovering how I should do this..

By what you're saying, I'm dealing with text in a container , the
Formatter function is what reads and activates the text (I'm guessing)
and creates the functions themselves... or does it.. is it just
creating another array of text... I think my brain melted...

as the main functions activates formatter as a new Formatter.. then...
formatter.pattern[] should contain the function itself..so instancing
formatter.pattern[0] (if 0 is the right array reference) should do
the trick?
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top