Creating Instantatable Objects Using Pure JSON? -- No Prototyping!

Apr 23, 2007

I've been pretty infatuated with JSON for some time now since
"discovering" it a while back. (It's been there all along in
JavaScript, but it was just never "noticed" or used by most until
recently -- or maybe I should just speak for myself.)

The fact that JSON is more elegant goes without saying, yet I can't
seem to find a way to use JSON the way I *really* want to use it: to
create objects that can be instantated into multiple instances without
prototyping. I've seen (and used) JSON for singleton object instances
-- this not a problem and this is how it works right out of the gate.

But given the following custom object written the past "normal" way, I
would like to write it in JSON format, and then be able to create new
instances from the one definition. Here's an example using the "old
way" most who have been writing JavaScript for years have seen:

function Two( x, y ) {
this.x = x;
this.y = y;
}

Two.prototype.sum = function () { return this.x + this.y }
Two.prototype.max = function () { return this.x this.y ? this.x :
this.y }
Two.prototype.min = function () { return this.x this.y ? this.y :
this.x }
Two.prototype.pow = function () { return Math.pow( this.x, this.y ) }

Now, I know I can get all "fancy" with the above and do either this:

Two.prototype = {
sum : function { return this.x + this.y },
max : function () { return this.x this.y ? this.x : this.y },
min : function () { return this.x this.y ? this.y : this.x },
pow : function () { return Math.pow( this.x, this.y ) }
};

Or this:

function Two( x, y ) {
// Properties.
this.x = x;
this.y = y;

// Methods.
this.sum = function () { return this.x + this.y }
this.max = function () { return this.x this.y ? this.x : this.y }
this.min = function () { return this.x this.y ? this.y : this.x }
this.pow = function () { return Math.pow( this.x, this.y ) }
}

(The later seems to work without prototyping...!)

But neither are really as close to pure JSON as I would like, so that
I can instantate those:

var hisPair = new Two( 11, 22 );
var herPair = new Two( 33, 44 );

What I'd like is a way in PURE JSON to be able to create the Two class
(as an example) using pure JSON. I've not seen anything yet on the
web that addresses this directly aside from some pages which require
you to include another JS to allow "deep embedding" of classes using
other helper "classes" (that are created the "old way" it seems), etc.

The best I've found so far on using pure JSON to create a class that
allows *multiple* instances is something like this:

function Two( x, y ) {

var class = {
x : x,
y : y,
sum : function { return this.x + this.y }
max : function () { return this.x this.y ? this.x : this.y }
min : function () { return this.x this.y ? this.y : this.x }
pow : function () { return Math.pow( this.x, this.y ) }
};

for (var element in class) this[element] = class[element];

}

Now *THAT* works, but it's still not as "pure" I would like. But it's
acceptable for now, I guess, since I *am* creating the entire "class"
as a JSON object, and I consider the outside function "wrapper" as the
necessary "constructor." But I keep wondering... There HAS to be a
better way.

I'm just wondering if anyone knows of a place that discusses JSON used
in situations like the above. Again, I've seen an ABUNDANCE of pages
and sites that discuss JSON in Singleton usage, but nothing that
discusses it as I am wanting here.

View 2 Replies


ADVERTISEMENT

Objects And Prototyping - Transform XML Into HTML In Different Views

Nov 8, 2009

I am ok with using objects creating classes if someone else defines, but when it comes to defining my own, I hit a nasty brick wall... I am using an XML/XSLT wrapper called Sarissa to help with programming a utility to transform XML into HTML in different views. For this to happen, I have created a Loader class which loads in XML required. I am aware of prototyping for binding methods to objects (as opposed to replicating the same method every time an instance is created)... The aim being I want to create a progress bar for the essential files that need to be loaded in. Presently I have them load in Synchronous mode just to get the utility working, which I know is poor, so would like to address it.

[Code]...

View 1 Replies View Related

Combining Two JSON Objects ?

Mar 25, 2010

I am currently generating two different JSON objects from Coldfusion. I've read several things about how to merge JSON objects but they always modify the first object if it has the same Key. What I want to do is append the second objects contents to the end of the first objects contents under the same key.

View 14 Replies View Related

Creating An Array Of Objects

Oct 3, 2011

As they all have the same property set but with different values I thought I'd try creating a servo object, the create an array of servo's but I don't think I'm getting anywhere fast. Heres what I have

<!DOCTYPE html PUBLIC "-W3CDTD XHTML 1.0 TransitionalEN" "http:www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http:www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
[Code]...

Once this is done and I've got all the servo objects created with their properties, I'm hoping to be able to search for all servo's with a set property i.e all servo's with servo.application = 1 would that be possible, if not I geuss I'd be wasting all our time trying to create classes I can't use the way I'd like.

View 14 Replies View Related

JQuery :: Creating Objects Within Each()

Oct 7, 2010

I have a tab compopenet object that works by passing a dom element to a constructor... which then finds all the relative elements to that element using .find() and adds the necessary behaviour. Now, since I have multiple tab components on the same page, I'm using each() to iterate through the dom elements and pass each into the tab component constructor function.

[Code]...

View 1 Replies View Related

JQuery :: JSON To Array (How To Know Number Of Objects)

Apr 4, 2010

I am writing this code
<script type="text/javascript">
$(document).ready(function() {
var obj = jQuery.parseJSON('{"a":"sss","b":"sss","question":"whi?"}');
//alert(obj.question);
});
</script>
How could I know how many objects variable obj has?

View 3 Replies View Related

JSON.stringify() Acceptable - Send All Of The Data From These Objects To PHP

Apr 27, 2010

I'm working on a very Javascript heavy application, and we've set up a number of different Objects to handle the data on the page. Very often, we'll need to send all of the data from these objects to PHP. Right now, we're using JSON.stringify. Are there any issues with this? It seem's like it does exactly what we need, but are there any downsides?

View 1 Replies View Related

Creating True Copies (of Objects) In JS (possible?)

Jul 20, 2005

Lets say I have this array:

a1 = [1,"2",new Array(2.5,3,3.5),4];

And I apply slice(0) on it, to create a copy:

a2 = a1.slice(0);

But this isn't a true copy. If I go a1[2][1] = 42, and then
alert(a2[2][1]) I will see 42 there too. I'm doubting, but I was
wondering if there is a general solution to this problem?

View 20 Replies View Related

JQuery :: Creating Objects From Within .each Question?

Oct 7, 2010

I have a tab compopenet object that works by passing a dom element to a constructor... which then finds all the relative elements to that element using .find() and adds the necessary behaviour.Now, since I have multiple tab components on the same page, I'm using each() to iterate through the dom elements and pass each into the tab component constructor function.[code]This works fine for my purposes, but after running my code through JSLint and researching here (URL...), I've been made aware that I'm pretty much throwing these objects away, since I'm not assigning them to a variable.My question really is; is this particularly bad practice under the circumstances? If I assigned the object to a variable within .each(), I'd surely be overwriting that varibale with each iteration and essentially doing the same thing?

View 1 Replies View Related

Dynamically Creating And Naming Objects?

Oct 13, 2010

I've got an input with a value. The input is called 'command2' and I want to send it's value to the 'rover2' object (although I don't know if that object exists yet). I test and say if(rover2){... and if not then I create the object/if so then I insert the value.Question is: I want to do:Code JavaScript:var rover2 = new Rover();but I want to pass the name of the new object by association, so in effect:Code JavaScript:var "rover"+i = new Rover();How would you do that? So that the objects and their names are generated dynamically (or [perhaps a better explanation] so that the string value of a variable can be used as the name of new variable/object)?PS Bonus marks: If I hold HTML fragments as an object and those fragments include inputs, is the value of the input collected as well? i.e. if I have

Code HTML4Strict:
<fieldset>
<input id="foo" type="text" />

[code]....

View 5 Replies View Related

JQuery :: Creating Objects On The Page To Load?

May 10, 2010

if I am loading content into a div using the get feature of jquery, and that content has some clickable objects in it will jquery recognize those as well? What I am doing is building a site that tracks bugs on our software. The site pulls the bugs from a mysql db and displays in a table format. There's an edit link which passes the record id number of that bug (via ajax) to a php file, where a form is filled in with the bug data and passed back, a hidden div is then shown with the form in it. Part of the code I pass back has a submit button and a close button (css styled anchors) and I'm using the same process on them as I am on the edit link but not getting any results. My script looks like this:

<script src="js/jquery.js"></script>
<script language="javascript">
$(document).ready(function(){
//hide edit form

[Code].....

how it works. Does it sneak peek the page after it loads and get all the objects into some sort of array? I'm thinking it is doing that and because I am adding other object through AJAX it is not seeing those, since the page is not being reloaded.

View 1 Replies View Related

JQuery :: Creating Multiple Objects In An Each() Loop?

Sep 16, 2011

I have to stuff multiple objects in global var populated in a foreach loop and seperated by a comma.The loop is:

// GlobalG.objects;
$('.class').each(function() {
var title = $(this).text();
var src = $(this).attr('src');

[code]....

View 2 Replies View Related

Creating And Managing Unknown Numbers Of Objects?

Jan 30, 2011

ok, so I can make an object, I can also make a new instance of that object and have it running around the screen at the same time.What I'm a bit confused about is how to go about things when I want X number of these objects.So instead of making objects seperately I need a loop. Instead of referring to each object seperately every time I want to redraw it, I want to loop through all instances of the object.Except I can't quite see how one goes about doing this.

for (i = 0;i <= 10; i++) {
var dot + i = new Dot;
}

[code]....

View 2 Replies View Related

Creating Frogger Game - Collision Of Objects

Mar 16, 2010

I am creating a frogger game. When the frog image collides with a vehicle image, I wish for the frogs position to be reset back to the start. Here is my code which is currently not working:

Code:
// Frog Collisions: (CURRENTLY NOT WORKING)
var cvehicle=new Array(0,
document.getElementById("car1"),document.getElementById("lorry1"),document.getElementById("car4"),document.getElementById("lorry2"),document.getElementById("car2"),document.getElementById("car3"),
document.getElementById("lorry3"),document.getElementById("car5"),document.getElementById("car6"),document.getElementById("car7"),document.getElementById("car9"),document.getElementById("car10"),
[Code] .....

View 3 Replies View Related

AJAX :: Request + Pass Json Objects To The Server? - Firefox Error: No Element Found

Feb 8, 2010

I'm making this ajax call:

Code:
url = "/GeoAdaptaApp/geoLogger/logGuiEvents?json="+aLotOfJSONStuff;
encUrl = encodeURI(url,"UTF-8");
new Ajax.Request(encUrl, {
method: 'get',
onSuccess: this.sendQueueToServerSuccess( this,logConsole ),
[Code]...

The JSON string seems correct (I checked it with a validator) and it worked on an Ajax.updater (but i need a request now). Firefox keeps telling me:

[Code]...

The call always end up in the onFailure block. Full request here: [URL] It's very strange, Is there a better way to pass json objects to the server?

View 1 Replies View Related

JQuery :: Wrap Pure Text Only?

Jul 28, 2010

I've a nested list which looks something like:

<li>This header of sublist
<ul><li>item1</li>
<li>header of subsublist

[code].....

Now I would like to wrap the text and only the text (i.e. 'This header of sublist', 'item 1', 'header of subsublist','item2.1') in a <span> tag. I tried $('li', his).wrapInner('<span class="test"></span>') but that includes the ul element as well, which I would like to exclude.regards,

View 3 Replies View Related

A Prevent Default On The <a Href> In Pure JS?

May 9, 2011

I am loading a link with ajax. When the link pops on the screen and I click it, I get redirected to my 404 page and my lightbox doesn't load. If the link pops in and I refresh my browser, then I click the link my lightbox will show up. How can I do a prevent default on the <a href> in pure JS? No frameworks.

View 4 Replies View Related

Prototyping

Aug 20, 2007

Curious about how prototyping works and if there is a workaround for my problem. Say I create a new function for Objects called 'keyExists':

JavaScript Code:
Object.prototype.keyExists=function(key)
{
   for(var tempKey in this)
      if(tempKey==key)
         return true;
 
   return false;

};

When I use this function, one of the keys in the iteration will be the new function that I have created.

The following example would alert the following: "key1", "key2", "keyExists". The last one is my concern. Obviously their are other browser-defined functions available for Objects that don't show up when iterating through the object. I thought Object.prototype.functionname was the methodology for that but apparently not. Is there a way to achieve the effect I am looking for? Code:

View 1 Replies View Related

IE Doesn't Like Prototyping

Feb 2, 2007

I'm having a problem with IE 7 (other versions didn't work either) when it comes to prototyping.

This is the code:

View 2 Replies View Related

HTMLElement Prototyping In Safari

Jun 3, 2005

We all know (and love!) the fact that Mozilla exposes element constructors (HTMLParagraphElement, for example) for prototyping. Turns out Opera 8 allows you do to the same thing.

So this leaves Safari, which has actually allowed you to do this since 1.0, *but* doesn't publicly expose the constructors. The below code exports constructors into public variables matching those in Mozilla and Opera 8:

/*
HTMLElement Prototyping in KHTML and WebCore
Copyright (C) 2005 Jason Davis, www.jasonkarldavis.com
Additional thanks to Brothercake, www.brothercake.com

This code is licensed under the LGPL:
http://www.gnu.org/licenses/lgpl.html
*/

if (navigator.vendor == "Apple Computer, Inc." || navigator.vendor == "KDE") { // WebCore/KHTML
function(HTMLConstructors) {
for (var i in HTMLConstructors) {
window["HTML" + i + "Element"] = document.createElement(HTMLConstructors[i]).constructor;
}
}({
Html: "html", Head: "head", Link: "link", Title: "title", Meta: "meta",
Base: "base", IsIndex: "isindex", Style: "style", Body: "body", Form: "form",
Select: "select", OptGroup: "optgroup", Option: "option", Input: "input",
TextArea: "textarea", Button: "button", Label: "label", FieldSet: "fieldset",
Legend: "legend", UList: "ul", OList: "ol", DList: "dl", Directory: "dir",
Menu: "menu", LI: "li", Div: "div", Paragraph: "p", Heading: "h1", Quote: "q",
Pre: "pre", BR: "br", BaseFont: "basefont", Font: "font", HR: "hr", Mod: "ins",

Anchor: "a", Image: "img", Object: "object", Param: "param", Applet: "applet",
Map: "map", Area: "area", Script: "script", Table: "table", TableCaption: "caption",
TableCol: "col", TableSection: "tbody", TableRow: "tr", TableCell: "td",
FrameSet: "frameset", Frame: "frame", IFrame: "iframe"
});

function HTMLElement() {}
HTMLElement.prototype = HTMLHtmlElement.__proto__.__proto__;
var HTMLDocument = document.constructor;
var HTMLCollection = document.links.constructor;
var HTMLOptionsCollection = document.createElement("select").options.constructor;
var Text = document.createTextNode("").constructor;
var Node = Text;
}

In Opera < 8, all elements inherit directly from Object(), so you can still prototype Object() if you need to do element prototyping. Internet Explorer's elements *don't* inherit from Object, oddly enough, so who knows with that browser. In any case, there you go. It also happens to work in Konqueror (AFAIK).

View 4 Replies View Related

Misunderstanding - Replace Prototyping With Own Array Type Based On Object

Dec 17, 2009

I'm apparently misunderstanding what I'm reading on prototyping. My main task is to squeeze some performance out of an app that's a bit slow on IE, and it looks like large Arrays and their overhead may be part of the problem. I'm trying to replace those with my own array type based on Object and extend it with helper functions like .length. Here's a munged sample of what I've tried:

[Code]...

View 3 Replies View Related

Prototyping Within Library - Extend Or Super-class The Native String Object

Apr 21, 2011

I have a few String prototypes such as String.prototype.EscapeReg = function () { return this.replace(/[-[]{}()*+?.,\^$|#s]/g, "\$&"); }; // Escapes characters for use with a regular expressionI also have my own class/ library which is used like this var adg = new AndyG_ns.ADG_Utils(); adg.StartClock('AndyClock','dd mmm yy hh:nn'); // etc.What I would like to do is to only add the prototype to my library (not to the global namespace). The end result I'm looking for is to use code such as:

var adg = new AndyG_ns.ADG_Utils();
var myString = new adg.AString();
var parsed = myString.EscapeReg();

In addition, I want to be able to also use/create my special string sub-class within my library. I suppose I'm saying that I would like to extend or super-class the native String object.

View 6 Replies View Related

Create A Youtube Video Inserter Script For Site - Prototyping An Element

Sep 16, 2009

I'm trying to create a Youtube video inserter script for my site. Long story short, I need to be able to prototype an HTML element and add a string into the element's innerHTML, like this:

myObj.insertVid(5sw2OvIgoO8);

I'm using jQuery to getElementsByClass("sip").

[Code]..

View 7 Replies View Related

Objects Under Mouse - Return An Array Of All Objects Underneath A Certain Point

Apr 17, 2011

Is there a way in Javascript or Jquery to return an array of all objects underneath a certain point, ie. the mouse position. Basically, I have a series of images which link to various web pages but I have a large semi transparent image positioned over the top of the other images. I want to find the href of the background image that the mouse pointer clicks over.

View 1 Replies View Related

JQuery :: Html Prototyping Template Plugin - Building The Templates Or Filling Them With Content?

Jun 13, 2010

I am working on a rapid html prototyping framework. This would allow me (as an interaction designer) to create html prototypes fast.I have been looking in templating plugins to help with building the templates or filling them with content. For example {{ lorem_ipsum }} would be replaced with a few paragraphs of random text. This is easy to do with several plugins.

However it would be great to be able to add a bit more flexibility by using functions like {{ loremWords(100) }} which would add 100 random words which are different for each replaced item. Or something similar to add a menu or other snippets.It is probably not too difficult but so far I have not gotten it working yet. And the pluginsI have found are either too complex, requires script tags or can't handle functions.

View 2 Replies View Related

JQuery :: Extending Objects With Internal Objects?

Sep 5, 2009

Is there a better way to extend object with internal objects?

$.fn.bestShow = function(s) {
var d = {
width: 0,
height: 0,
order: "numeric",
orderBy: "",

[Code]...

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved