Copying All Prototype Functions AND The Constructor From One Object Into Another Object
Apr 5, 2011
How would I go about copying all the prototype functions AND the constructor from one object into another object, and then call them?
I know I can use this.example.call(this), but that's not an acceptable solution. I want to deploy this style over dozens, potentially hundreds of objects.
I have a requirement to override the constructor of a javascript class. I have to execute the current constructor code and then call a javascript method. Basically, I need to execute a javascript method whenever an object of that particular class is created in addition to whatever is being done now. I do not want to copy and paste the current constructor code and override the initcomponent method. I am looking for an alternative for that.
I had read from books that the constructor property of object is inherited from its prototype. And the prototype can be changed dynamically. New property can be added to or deleted from object even it was created before the prototype change.
I'd like to make a Degree object that inherits from the Number object and uses the Number constructor but adds a .rad() method that returns the value in radians.
If I do something like:
It generally works but I don't get Number's methods like toString and toPrecision.
I need to detect the "name" of the constructor of an object. For example, myArray.constructor === Array is true, but I want something where that === 'Array' is true. I've heard of Object.constructor.name, but it's not widely compatible. (preferably without involving regular expressions)?
Now, the code worked fine in both script. (i.e., no more error.) So far as I can tell, the only differences between the first and next version of Book.js are the comments and the whitespace. Am I missing something? Can the comments or whitespace somehow effect the logic with which this code has been executing? If I get an error like this again, is there an easier/better way to fix it than to essentially retype the code?
How to extend the constructor for the date object of the javasccript so that whenever a call is made to the constructor, I want to perform a particular action? Basically how to define wrappers for default javascript methods or objects like Date() so that I can perform some action and then invoke the original method? So basically if I have something like var a = new Date(); I want it to (say) alert the value of the date everything Date() is called and then execute the default date constructor.
I need to create a constructor for a computer object. This object must have three properties: speed, and mem_live mem_dead. Then I need to create a new object using this constructor and then have its properties displayed on the screen.Look at what I'm up to so far:
function Computer(speed, mem_live, mem_dead) { this.speed = speed;
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?
function show() { var s = '' ; for (var i = 0; i<arguments.length; s += ' '+arguments[i++]) ; typeof(window) != 'undefined' ? window.alert(s) : WScript.Echo(s) ; }
function f(){} show('delete(f):',delete(f)) ; // false
Sorry, bad title. Anyway, is there a way to pass the arguments to an object instantiated via a constructor using the arguments object and have it expanded, so to speak, so that it doesn't appear as a single argument? I'm sorry, this explanation is just atrocious, but I can't think of exactly how to word it. Maybe an example...
Take for instance Function.apply. It takes 1-2 arguments, the first being the object to use as the context, and the second being either an array or an instance of the arguments object which are to be the arguments for the function. I want to do something similar but I want to also basically use the new operator so that I get back an object.
Here's a snippet of some of my code, maybe this will help:
I'm sure this question has been asked many times before, but I can't seem to get the correct terms to find the answer in Google.I understand basic JavaScript inheritance. However, everything I've read makes the inheritance work something like this:
Code JavaScript: var ChildClass = new Function(); ChildClass.prototype = new ParentClass(); ChildClass.constructor = ChildClass;
That's fine, but what happens when ParentClass's constructor needs parameters?As an example, look at the following code:
Code JavaScript: function Furniture(color){ this.color = color;[code)....
How could I "copy" Furniture's constructor into the Chair object? Better yet, how could I copy Furniture's constructor and then add some functionality to it?
The problem is I keep getting an error along the lines of: TypeError: Result of expression 'house' [[object Object]] is not a constructor. It seemed to work when I wasn't passing the other objects as parameters in the constructor. I just created and assigned them later. As in:
The code below is jquery, and uses jquery's extend() [URL] function, but I don't think that the issue has anything to do with that... does it?
$(document).ready(function(){ var obj1 = { "name": "spud" ,"age":32 } var obj2 = obj1; $.extend(obj2, {"location": "UK"}); console.log(obj1); });
By my reasoning, obj1 shouldn't have location set. But it does. Is the line var obj2 = obj1; not making a copy of the original and leaving the original intact? Is it instead making some sort of reference to it? Why is changing the copy having an effect on the original??
I have static object and I want remake it to a constructor with property 'length' which would be not enumerable in for loop... This is my object and how I generate it
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.....
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:
QUESTION ONE: When is use of the jQuery prototype object appropriate, and when is it not?
BACKGROUND:I am still trying very hard to identify the error that is prohibiting me from incorporating a jQuery plug-in into my site in a manner similar to the way that the author of the plug-in has incorporated it into his.Although I have sought consultation with the author, he appears uninterested in working with me.My still fledgling knowledge of jQuery tells me that the author of the plug-in and my implementation of his plug-in are constructed differently.Whereas I use jQuery's prototype property to reference my method and then assign my method anonymously to my HTML document as follows: $().myJQMethod().The author of the plug-in does something very different.I have extracted from the author's plug-in and my implementation of it. Links to the source pages have been included.
CONSTRUCT A (The jQ_Impromptu Plug-In): (function($) { $.prompt = function(message, options) { })(jQuery);[code]...
QUESTION TWO: Although I am able to implement the author's method, it is not performing as it should. When the alert box appears with focus the hosting HTML page is suppose to show through with dimmed opacity.My implementation does not achieve this effect. Firebug has alerted to me to the following breakpoint, but I am poorly unable to interpret it.
I find the JavaScript's Object.prototype and getter/setter mechanism very nice. However, I need some help with extending an object with getters/setters in the derived class. For example:
A : function () {}
A.prototype = { a : null,
get a : function () { return a++; } };
B : function () {}
B.prototype = new A;
Now, I would like to define a getter/setter in B. How do I do that? There is no way to use the same syntax as in "A.prototype = ..." above.
I was hoping I could do something like this in javascript:
var a = {x:1, y:2}; var b = {x:2, z:3}; b.prototype = a;
And then:
b.x => 2 b.y => 2 b.z => 3
The purpose is to set up "b" as an override of "a".
I know it doesn't work that way because "prototype" is not a magic property on any object but constructors only. Is there a way to implement the above without involving a constructor?
function superDuperObject(range) { this.startContainer = new superDuperObjectStartContainer(range); }
function superDuperObjectStartContainer(range) { this.calculatedContainerNumber = doSomethingAndGetSomethingBack(range); return range.startContainer }
function doSomethingAndGetSomethingBack(range) { return someCoolInformationThatICalculateInThisFunctionTha tNeedsTheRange; }
var myObject = new SuperDuperObject(DOMRangeObjectThatIDefindedEarlie rInCode);
// Show me the startContainer of "DOMRangeObjectThatIDefindedEarlierInCode" alert(myObject.startContainer);
// Show me the calculatedContainerNumber that I get from a function that does stuff with the range passed to it alert(myObject.startContainer.calculatedContainerN umber);
in UML? I just found that UML is actually very class-based. It's difficult to depict the relationship between a constructor function, its prototype property, and objects that are created using that constructor function using UML.
Have any of you ever attempted this? How would you do it?