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


ADVERTISEMENT

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

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

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 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

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







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