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
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?
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
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.:
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.
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".
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:
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.....
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:
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.
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?
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:
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.
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?
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?
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.
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?
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:
Can anyone tell me what the difference is between these two protoype methods? They are extensions of Javascript's Function class.
Is bindAsEventListener just used to be compatible with IE's Event model? I'm thinking the only difference between the 2 methods is that bindAsEventListener passes the event into the function.
In my php script (which is located at URL 'ships'), I am just getting the value of 'variable' from the $_GET super global but I obviously want that value to be dynamic and equal to the value selected in the dropdown.
So the missing piece is passing the selected dropdown value from HTML / Javascript to PHP. Anyone know how to do that?
I am wondering why the following works, on IE6, but with an error : "Not implemented".
function TEST(){} TEST.prototype.Initialize = function() { var mImage = new Image(); var mDate = new Date(); var start = mDate.getTime(); mImage.onload = this.Alerting(start);//WORKS with ERROR "Not implemented" //mImage.onload = function(){this.Alerting(start);}//ERROR "Object doesn't support this property or method" mImage.src = "winxp.gif"; } TEST.prototype.Alerting = function(i_string){alert(i_string);} var mTest = new TEST(); mTest.Initialize();
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'