Confusion While Understanding Prototype
Oct 4, 2007
I tried to write sample code to get understanding of javascript's
prototypal inheritance (along with the variety of function calling
choices.. )
During the process, I got myself throughly confused.
Can anyone explain following behavior [NOTE: Needs firefox+firebug.
otherwise replace all console.* to alert or equivalent]
here is my code:
<html>
<head>
<script type="text/javascript">
function MyClass(){
if(String(this) == '[object Window]'){
console.debug('Are you sure to use this object in global scope?? Or
you just missed using 'new' keyword?');
}
this.method = function(){
console.info('in MyClass.method');
}
this.param1='param1'
}
function caller(){
var execClsDirectly = MyClass();
var execClsConstructor = new MyClass();
// execClsDirectly would be null since MyClass does not have any
return statement
if (execClsDirectly) execClsDirectly.param2='added global fn call'
execClsConstructor.param2='added to obj instance'
var clsRef = MyClass;
clsRef.param2='added to class definition'
MyClass.prototype.param2='added to class'
console.log('execClsConstructor param2: '+execClsConstructor.param2);
console.log('outer param2: '+MyClass.param2);
var updatedClsConstructor = new MyClass();
console.log('updatedClsConstructor param2:
'+updatedClsConstructor.param2);
}
</script>
</head>
<body onload="caller();">
</body>
</html>
the output:
Are you sure to use this object in global scope?? Or you just missed
using 'new' keyword?
execClsConstructor param2: added to obj instance
outer param2: added to class definition
updatedClsConstructor param2: added to class
I of course understand the first line of output, but.. confused as to
same param2 added into MyClass gets added into three different levels.
Can anyone elaborate this?
View 2 Replies
ADVERTISEMENT
Jun 27, 2005
I noticed the recent move to standards compliant JavaScripting. All my code (XHTML and CSS) is coded to standards, so I don't want to start learning outdated material that wont sit right on with web standards.
What is the difference between JavaScript, ECMAScript and the DOM ?
I found a cheap JavaScript book at a bookstore (in the bargain bin) that looked quite good despite the low price. Will I most likely be learning invalid techniques if I use this book as a starting point?
I feel lost with all these different terms that seemingly mean the same thing: JavaScript.
I want to learn it, but I don't want to waste my time either. What is the best way to learn standards compliant JavaScript? Can I continue with what I have already (I have the JavaScript bible and another book I forget the title at this time) then slightly alter my knowledge to be standards compliant, or is it a whole new ball game?
View 5 Replies
View Related
Jan 28, 2011
Can someone tel me the code to put a dropdown box showing default values as shown below using script ..i,e by enclosing them in document.write in script
<CODE>
<SELECT NAME="choice" >
<OPTION VALUE="1">one </OPTION >
[code]....
View 1 Replies
View Related
Jan 29, 2003
In the following code, I'd expect the innerText of my DIV (ID=count) to increment after a delay of 1000 milliseconds during each iteration of the FOR loop.
However, it's not being re-written until the loop ends.
The window.alert statement is simply to prove that the value I'm assigning to it is incrementing. Code:
View 8 Replies
View Related
Jul 31, 2011
im using the autocomplete function to retrieve data values from a database using jquery/ajax/jsonafter web service call my data.d is shown in this format
{ 'Suggestions' : [ { 'Value' : '48', 'Label' : 'Chocolade'} , { 'Value' : '41', 'Label' : 'Jack's New England Clam Chowder'} , { 'Value' : '27', 'Label' : 'Schoggi Schokolade'} , { 'Value' : '19', 'Label' : 'Teatime Chocolate Biscuits'} ] }
[code]....
View 4 Replies
View Related
Jul 27, 2009
I'm confused by firebug's output under the tab 'Net' > All:I post this in the JS section, as I'm sure many js coders use firebug and my question pertains to firebug.I have an image which appears several times on my site - the same image each time, just displayed in different places.For each time the image is displayed, I see a separate call to fetch it from the server, with info on 'Queuing, waiting for response, receiving data' etc, all with the number of milliseconds shown.It looks like it's being downloaded multiple times rather than only once.
View 2 Replies
View Related
Nov 25, 2011
According to ECMAScript, the root of the prototype chain is Object.Prototype. Each object has an internal property [[Prototype]] that could be another object or NULL.... However, it also says that every function has the Function prototype object: Function.Prototype, it confused me, because a function is an object, for a function object, what is its function prototype and object prototype..For example:
var x = function (n) {return n+1;};
what is the relationships of x, Object.Prototype and Function.Prototype
View 5 Replies
View Related
Dec 14, 2009
I am trying to get to the bottom of javascript object, prototypes etc. I have a fairly good grasp of it, but I get confused the closer I get to the base object.prototype. FIrst of all, I was under the impression that all objects descend directly from Object. But some objects (like Array) seem to inherit properties and methods from the function.prototype. So does this mean that the chain is like this:
object -- function -- array Second, I noticed (on the mozilla javascript reference site that object.prototype inherits properties and methods from function.prototype and vice versa!? How can this be? I must be missing something important about understanding the chain?
View 24 Replies
View Related
Aug 24, 2005
I can use "with" like this:
function MyObject(message)
{
this.message = message;
}
function _MyObject_speak()
{
alert(this.message);
}
with (MyObject)
{
prototype.speak = _MyObject_speak;
}
I was wondering why I can't use "with" like this:
with (MyObject.prototype)
{
speak = _MyObject_speak;
}
View 8 Replies
View Related
Jul 23, 2005
One of the complaints about prototype.js (google for it if you're not
familiar with it) is that it's poorly documented. I have this inkling
that the key to understanding prototype.js is in the bind function.
The problem with Javascript is that the "this" operator is poorly
overloaded and it is often hard to understand in the context of
object-oriented javascript
So, let's start with the definition:
Function.prototype.bind = function(object) {
var method = this;
return function() {
method.apply(object, arguments);
}
}
As I read this, it states that all functions (which are themselves
objects) will, in the future, have an associated method called "bind".
The function() function, so to speak, simply instantiates a Function
object with the parameter list and then evals the statement, sticking
the resulting execution-tree in the current code frame.
The "this" there refers to the function object associated with the call
to bind(), right? But the word "arguments" there refers to the
arguments passed to the function object *generated by* the call to
bind().
In every example within prototype.js, bind() is called either in a
constructor or within a method contexted to a javascript object, and is
always called with "this" as its argument, e.g.:
this.options.onComplete = this.updateContent.bind(this);
As I read the code it seems to be stating that the "this" object
referred to within bind()'d functions are being coerced into always
referring to the current instantiated object.
But isn't this always the case anyway? Is this a rather confusing
attempt to ensure "this" purity whereby the call
method.apply(object, arguments)
is forced to always have the reference to the containing object present?
I think I've got it. Bind() generates uniq functions that contain live
references to the objects to which they belong, such that the function
object can then be passed to setTimeout() or onMouseOver(), handlers
that accept functions but not objects.
View 8 Replies
View Related
Mar 15, 2006
Lets say we run: window.alert = function() { };
Is there anyway to 'restore' the original alert() method or is it gone
forever?
I know you can do window.alert = Window.prototype.alert, but lets say
you also set Window.prototype.alert = function() { } or lets say we're
in Opera, which doesnt have a Window "class".
View 3 Replies
View Related
Jun 5, 2006
I would like to set up an event observer outside of an object, so I
can't use this.bindAsEventListener. How can I pass the correct object
reference?
I tried something like this, and various other variations, but no luck.
This works when I set it up from inside the object, using "this.",
Event.observe(targetId,'click',targetId.select.bin dAsEventListener(this),false);
View 28 Replies
View Related
Jan 11, 2007
I wanted to add an object as a prototype to separate my methods more
nicely, however, I ran into a couple of problems. Apart from the
obvious "scope" issues I found that any instances of my class shared
the objects methods and properties.
I realise (now) that this is actually how prototypes work, they share
functions and objects rather than create new instances of them for
every "class", but is there any way around it? (or shouldn't I be doing
things like this at all?) Code:
View 2 Replies
View Related
Jan 23, 2007
I am working on my own pop up calendar, mainly because the one I am currently using crashes the Safari browser at times.
So, I want to verify that what I am doing will work, in that I want to be able to have multiple calendars open at the same time, each independent of the other.
So, I start it off with:
var Calendar = {
dateSelected: null,
topPos:null,
leftPos:null,
somefunction:function(e) {
...
}
};
If I create more than one calendar object, will they have their own variables, in that the dateSelected, topPos and leftPos will be unique to that instance?
Or, is there a better way to do this, that is cross-platform.....
View 8 Replies
View Related
Mar 4, 2007
In my research in the javascript language I have encountered problems
with implementing prototype inheritance while preserving private
methods functioning properly. Here is an example: Code:
View 2 Replies
View Related
May 16, 2007
I want ask you if, for a web portal/application, is better prototype or Jquery? I don't want to innesc some type of flame, but after the announce that drupal use JQuery and that the new Wordpress
2.2 use Jquery I ask myself if my choice of use prototype.js is the bettere choice.
View 5 Replies
View Related
May 17, 2011
I have <div id ="changeable"> with some html in it. I have a link that calls the function to replace the info in the div. The problem is that the "creative_development.inc" file is added to the top of the div and does not replace the content. How do I replace the content, and not just add content?
View 1 Replies
View Related
Nov 20, 2007
I'm trying to make an addEvent function that will automatically attach itself to the object using a class.
My question is how can I add the function so when I write obj.addEvent("click",myfunction); it will add the event?
Here's my current function:
this.prototype.addEvent = function(type,fn)
{
if(window.attachEvent)
this.attachEvent("on"+type,fn);
else if(window.addEventListener)
this.addEventListener(type,fn,false);
}
View 4 Replies
View Related
Dec 13, 2007
I need help to workaround the following problem:
First, about the environment:
- The web pages automatically generated.
- Pages can be inserted to each other
- When a subpage to be inserted is being generated, it does not know will it be inserted or not.
- That because each subpage uses the <script src=...> tags to load the script it needs.
- in the external script files I'm trying to protect it from repeated execution checking (if(){}) the value of a variable which is created after in the script.
Everything worked fine until I tried to use the .prototype to declare the method of my objects.
In the Internet Explorer, I got "Object does not support this property or method", on the access to a prototype function, because the prototype of my object was deleted by itself after the second load of the external script. Code:
View 4 Replies
View Related
Apr 9, 2006
This function will return an array of the elements in a page that contain a certain attribute, you can also give it a value that the attribute has to match, a tag name that the element has to match and a parent element.
I know there are other functions for doing this, but this one is written for use with the prototype (http://prototype.conio.net/) JavaScript library, in fact its really just a modified version of the getElementsByClassName() function that’s part of prototype.
document.getElementsByAttribute = function(attribute, value, tagName, parentElement) {
var children = ($(parentElement) || document.body).getElementsByTagName((tagName || '*'));
return $A(children).inject([], function(elements, child) {
var attributeValue = child.getAttribute(attribute);
if(attributeValue != null) {
if(!value || attributeValue == value) {
elements.push(child);
}
}
return elements;
});
}
Usage is pretty simple, this will return all elements with a width attribute:
View 2 Replies
View Related
Nov 13, 2005
I try to get a xml-response with prototype. for e.g. i have the following code:
<html>
<head>
<title>Test Ajax</title>
<script src="prototype-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var getXML = function()
{
var myAjax = new Ajax.Request(
"bsp.xml",
{
method:'GET',
onComplete:showXML
}
);
}
var showXML = function(r)
{
var names = [];
var root = r.responseXML.getElementsByTagName('personal').item(0);
// root
for (var i = 0; i < root.childNodes.length; i++)
{
var node = root.childNodes.item(i); // mitarbeiter
names.push(node.childNodes.item(1).firstChild.data);
}
$('xml').innerHTML = names.join("<br />");
}
//]]>
</script>
</head>
<body>
<p>
<a href="#" onclick="getXML();return false;">TEST</a>
</p>
<div id="xml">
</div>
</body>
</html>
xml-Data:
<?xml version="1.0" ?>
<personal>
<mitarbeiter>
<vorname>John</vorname>
<name>Brown</name>
</mitarbeiter>
<mitarbeiter>
<vorname>Matt</vorname>
<name>Blue</name>
</mitarbeiter>
</personal>
This works fine in IE but in Geko-Browser i got nothing back, because this browsers build the DOM with linebreaks and whitespace. Do you have any ideas what is wrong in my code and how to get propper DOM?
View 1 Replies
View Related
May 16, 2006
Having read through Sergio Pereira's Prototype documentation I came across the grep command within Enumerable object.
This is something that looks very interesting. Such as being able to quickly select only the elements you want using a regular expression.
But for the life in me I can seem to get it to work.
Anyone a bit more up to speed with Prototype care to have a look?
var elementList = Form.getElements("just-a-form");
nodes = $A(elementList);
var localElements = nodes.grep( /image/, function(node){
return node.id
});
View 4 Replies
View Related
Oct 1, 2006
I'm using Prototype's AJAX to get the "alert" the response of a simple PHP script (a script that echoes a single line), but it doesn't seem to work. Here's the code:
function init()
{
var url = "http://localhost/script.php";
var myAjax = new Ajax.Request(
url,
{
onSuccess: showResponse
});
}
function showResponse(response)
{
alert(response.responseText);
}
window.onload = init;
The code has no errors, but still doesn't show the response even if i add an onFailure event handler. What could be wrong?
View 5 Replies
View Related
Dec 1, 2010
OK, simple script that currently takes an array of ID values (hard coded) and then iterates through them to fade in and out creating a slideshow.
The obvious problem is I have to hard code the number of DIVs in the slideshow. What I really want is the code to get the child elements of the container I pass it and then iterate through them dynamically.
[Code]...
View 7 Replies
View Related
Feb 27, 2006
I am trying to use the sortBy function in the prototype.js
library. Nevertheless, I can't work out what to include in the iterator
function. Can someone shed some light on this?
View 3 Replies
View Related
May 25, 2006
I've created a new class using prototype.js. After I make the ajax.request all references to this.myClassMethodorVariable are lost. Does the ajax method blow out the object persistance? I'm fairly new to OOP javascript so could be (and probably am) overlooking some detail. Code:
View 45 Replies
View Related