Difference Between Privileged Methods And Object.create
Apr 13, 2010
I'm attempting to understand the use of privileged methods when used with Object.create. I put together a quick demo (code below) that shows what happens when I use Object.create to create a new object based on one that has a privileged method. The outcome is not pleasant, as changing the value in the first object also changes it in the second. Unless I am reading Crockford's articles incorrectly, this makes Object.create almost useless for anything but objects that have only public members.
I've seen many JavaScript programmers use closures extensively to create private members, and that still holds to be a good programming practice. However I still can't find an elegant way to create inheritance in combination with closures in JavaScript, given downfalls such as the one I mentioned above.With all of that said I still think Crockford has a nice way of programming, creating factory functions that produce objects, staying away from the prototype property and making the language look more functional.
Here's the code to demonstrate what I'm referring to. Firebug needs to be enabled to view the console.debug output, otherwise convert them to alerts.
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
Can assign a new function to a built-in object in Firefox:
But IE and Opera don't have a MouseEvent or HTMLElement that can be set up in the same way. Can you do this in IE or Opera, or just Firefox, and maybe Webkit?
I have a javascript function that needs to access methods of a java object(localTag). In my JSP I'm trying to include hidden fields for the Strings returned from the getter method calls of the object like so:
If I can do something like this, what is the correct syntax and how do I access this property in my javascript? When I have a hidden field that is just a String, I access it in the javascript like "document.getElementById("theString").value" and it works fine, but I can't seem to find how to access the String value of a method call.
When you use addEventListener (or addEvent in IE) to call an object method, does it call it with the correct this parameter?
The ECMAScript reference has a lot to say about the caller using Function.prototype.call or Function.prototype.apply and passing the correct this pointer for the context, but how does addEventListener determine the correct this pointer. Or does it just punt and pass the global context, thus making it impossible to refer to the object this in an object method used as an event listener?
I have more of a programme design related question here: I have an object with 2 methods, those two methods are supposed to be called repeatedly one after the other (e.g. by setInterval())
the problem with this code is that the execution time of those methods may increase depending on the processed data (an array with several hundred elements or more) so it may be, that the execution time of both methods exceeds the given repetition time. I could solve that by placing a call to the next function in each method, but I wonder if that is good design or not (tight coupling)
code for method2() is analogue Obj.prototype.method1 = function () { /* working code here */
When a custom object is created in javascript, then methods need to be defined for this object. Still every object has toString() method available to them, even if it is not defined. How is this method available to all the objects ? Is it inherited from some root object ?
Is sort() method available to all the objects ? What are the other methiods available to all objects ? Where can I get list of available methods ?
I am a little confused how the memory for objects is allocated in JavaScript. David Flanagan, in "javascript: The Definitive Guide," states that each property of a class takes up memory space when instantiated. So space is created for aMethod three times in this example: Code:
Do many programmers remember most of the object properties and methods or do they use IDE or references to find those specific objects. I'm starting to learn Javascript and seeing all the different type of objects available can be depressing. :(
Well, I've been working with JS for three years and have a great experience here. But! I still have no really acceptable answer to the following question:
What is the principle difference between declaring methods/properties in the constructor function body and via prototypes.
Are there any real GURUs? Let's discuss the issue.
1) is getRule a local variable or global variable, as it has no var keyword, yet it is an inner function of Validation? So without var, I think global, but being an inner function, I think local. So I'm not sure which.
2) In this line of code: var rule = $.Validation.getRule(types[type]), getRule returns rules, which is just a local variable in Validation. I always see that you return functions, but how does returning a local variable that's just an object literal and not a function be able to return true or false? Now the value of rules is an object literal, and this object returns true or false. So we are basically allowed to use return keyword with local variables that are object literals and not functions?
3) In this line, is foo(age) being called, or is it just being assigned to bar OR is it being called and THEN assigned to bar: var bar = foo(age);
4) Now for the most confusing: age is obviously an object reference as opposed to a literal in the example. Does that make a difference in regards to closures? Note that I read a number of books, including JavaScript Programmer Reference and Object Oriented JavaScript and jQuery cookbook, which compare primitives vs reference types and how primitive types store directly in memory whereas reference tpyes reference memory, so if one reference changes, they all change where primitive remains ingrained. But when assigning a function as a reference like this, how does that affect the object "age" when passed into bar?
Code: function foo(x) { var tmp = 3; return function (y) { alert(x + y + tmp); x.memb = x.memb ? x.memb + 1 : 1; alert(x.memb); }} var age = new Number(2); var bar = foo(age); // bar is now a closure referencing age. bar(10);
I have an object on the document element that allows for other components to register with it, i have a custom event something along$(document).bind("register",function(thechild)..So in the child object when they are created i call$(document).trigger("register",this);And indeed i get the DOM object. However i'm looking for the plug in object, i want to be able to call methods on the passed childobject and access it's Config.Does that make sense? How can i write a plug in that is applied to various objects that also registers itself with an 'overseer' object on the document element in such a way that i can allow that overseer object to call methods on any registered child objects?
Is there any way at all to create a new template object that inherits from the built in Date object so as to be able to add new methods to that child object without adding them to the built in Date object? I've tried everything I can think of and as far as I can tell it keeps referencing the Date function instead of the Date object and so doesn't work.
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 know I can use the functions like parseFromString() to access and modify an existing XML documents, but is it possible to use JavaScript to create a blank xml document?
I want to take data entered into the client, package it up as XML and use ajax to transmit it to the server. So far I've written my own class to create a simple xml document, but I'd like to do it properly with the inbuilt functions.
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal.
function Client() { } Client.prototype.fullname = "John Smith"; var s = "Client"; eval("var o = new " + s + "();"); alert(o.fullname);
Note: I want the type name of the object to be represented as a string (in this case "Client" -- this is a non-negotiable requirement).
I also tried the following (which appears to fail):
function Client() { } Client.prototype.fullname = "John Smith"; var s = "Client"; var o = new Object(); o.construct = s; alert(o.fullname);
eval() is handy but not future-proof, so any suggestions would be welcome.