Private Methods In Object Literal?
Aug 18, 2010i have something like this:
Code:
var o = {
f1:function(a) {
[code]....
i have something like this:
Code:
var o = {
f1:function(a) {
[code]....
I have some confusion about the scripts below:
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($) {
/*Validation Singleton*/
var Validation = function() {
var rules = {
email : {
check: function(value) {
if(value)
return testPattern(value,".+@.+..+");
return true;
}, .....
$.Validation = new Validation();
})(jQuery);
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 the following code:
A = {
"value": "a",
};
B = A ;
B.value = "b";
alert (A.value); // => b
I expected A.value to be "a". how can I change values of a new object, without changing the parent-object?
I want to be able to set a property on a private object by giving the not notation path to the value. The difficulty is that this object is within closure so I can't access it directly to set the value the normal way (eg. dot.notation.path = 'new value'). This seems weird but I can't think of the obvious way.
Example:
// setter function
function set(path, change){
var privateObject = {
a: 'a',
[Code]....
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 created a method for displaying an object's properties:
As long as renderfunction = false, the object is fine coming out of this function.
However, if I change renderfunction to true, all my properties become undefined. Why isn't this working as I expect it to?
Below I declared a basic object literal with 2 methods. The
"doSomething" method is call from the "useDoSomething" method but the
call is only sucessful if I use the "this" keyword or qualify the call
with "SomeObj".
Can someone describe why this is happening?
var SomeObj = {
doSomething : function()
{
return 'Did something'
},
useDoSomething : function()
{
//Doesn't work - doSomething() no defined.
return doSomething();
//Works
//return this.doSomething();
//Works
//return SomeObj.doSomething();
}
}
alert(SomeObj.doSomething());
alert(SomeObj.useDoSomething());
I have read this from the interentfunction getArea(radius)
Imagine we want to create lots of circle objects using our object as a base - if we declare using the object literal, were not able to do that.
But the new operator can do that - basically allowing us to re-use a user-defined object.
can't understand on the above why we declare using the object literal not able to do that.
I am having a hard time getting the javascript file to run any of the functions after I've changed my original script to object literal format. Please tell me why it isn't working! I have attached a zip format of my code.
View 4 Replies View RelatedI'm declaring a class in OLN:
Code:
The problem is that in IE, when I call myfunc() it complains with the error: Class is undefined
I'm not sure what I'm doing wrong. Class is declared in as much global scope as possible, and that shouldn't even matter in JS.
I have this below code snippet
I have an object myscript and from within the method validate, i want to call the method init. so is there there anyway that I can call the method init without having to prefix like this: myscript.init();
I tried this.init(); but it's not working
Code:
Consider the following:
Code:
myObjLit = {
prop1:null,
[code]....
I'm using the standard module pattern and the problem is once you set a private variable, trying to test that object independently becomes a nightmare as the next test is polluted by the actions of the previous.So, the options are to have some reset method (which is horrible), setters on everything (defeats the point) or delete object and re-load script (hideous).
View 2 Replies View RelatedI 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:
<html:hidden name="FrmCustomerHolding" property='<%=
"localTag.getTagName()" %>' />
<html:hidden name="FrmCustomerHolding" property='<%=
"localTag.getTagValue()" %>' />
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())
window.setInterval(function() {
obj.method1();
obj.method2();
}, 100);
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 */
[Code]....
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:
Let's say I have a Javascript object that looks like this:
{
events: {
"comments:added": this.add,
"comments:removed": this.remove,
"comments:fetched": this.addAll
}
}
I'd like to add this method to it, either manually, using $.extends() or _.extends():
[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. :(
View 2 Replies View RelatedCode JavaScript:
function dropdown() {
}; dropdown.prototype = {
sele: $('.select'),
city: [],
scope: [],
type: [],
init: function(json) {
if(json.location.length) {
for(var i=0; i<json.location.length; i++) {
this.city[i] = json.location[i].city; .....
this.scope[i] = json.location[i].scope;
}}.....
getUrl: function() {
return location.pathname+location.search;
},
setUrl: function(paramType, param) {
return this.QueryString(paramType) ? this.setQueryString(paramType, param) : this.getUrl()+"&"+paramType+"="+param;
}};
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.
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() {}
[code]....
What are the different methods of selecting some of the values in the json object below:
what different selecting methods can you use to get the date, or the views?
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?
View 2 Replies View Relatedwhat is the best way to setup one literal to be the prototype for another?So far I can only think of making a copy into a constructor.
Code JavaScript:
var meths = {
// creates a constructor function populated with a shallow copy of child
// the prototype for the new instance is set to the parent.
[code]....