Tagged Questions
545
votes
20answers
353k views
Length of Javascript Object (ie. Associative Array)
If I have a javascript associative array say:
var myArray = new Object();
myArray["firstname"] = "Gareth";
myArray["lastname"] = "Simpson";
myArray["age"] = 21;
Is there a built in or accepted best ...
40
votes
3answers
16k views
How to return value from an asynchronous callback function?
This question is asked many times in SO. But still I can't get stuff.
I want to get some value from callback.look at bellow script for clarification.
function foo(address){
// google map ...
25
votes
2answers
3k views
Array Like Objects in Javascript
I'm wondering how jQuery constructs it's array like object. The key thing I'm trying to work out is how it manages to get the console to interpret it as an array and display it as such. I know it has ...
26
votes
3answers
6k views
What are the differences between JSON and JavaScript object?
I am new to JSON and JavaScript object.
Can anyone please explain the differences between JSON and JavaScript object?
What are their uses?
Is one better than the other? or it depends on the ...
116
votes
7answers
77k views
Create an empty object in JavaScript with {} or new Object()?
There are two different ways to create an empty object in JavaScript:
var objectA = {}
var objectB = new Object()
Is there any difference in how the script engine handles them? Is there any reason ...
3
votes
3answers
138 views
Organize prototype javascript while perserving object reference and inheritance
I have build a large application using javascript prototype and inheritance.
But I am having a hard time organizing my code.
For example I have a class carousel which has many functions like this:
...
4
votes
7answers
1k views
Are Javascript arrays primitives? Strings? Objects?
Are arrays merely objects in disguise? Why/why not? In what way(s) are they (such/not)?
I have always thought of arrays and objects in JS as essentially the same, primarily because accessing them is ...
15
votes
3answers
212 views
What is the behavior of typing {a:1} giving 1, and {a:1, b:2} giving an error in a Javascript console?
The following will show in Firebug or in jsconsole.com or in other Javascript interactive console:
>>> foo = { a : 1, b : 2.2 }
Object { a=1, more...}
>>> foo.a
1
>>> ...
38
votes
6answers
38k views
Number of elements in a javascript object
Is there a way to get (from somewhere) the number of elements in a javascript object?? (i.e. constant-time complexity).
I cant find a property or method that retrieve that information. So far I can ...
39
votes
1answer
78k views
Encoding Javascript Object to Json string
I want to encode a javascript object into a json string and I am having considerable difficulties.
The Object looks something like this
new_tweets[k]['tweet_id'] = 98745521;
new_tweets[k]['user_id'] ...
5
votes
3answers
4k views
using reserved words as property names, revisited
Can a reserved word be used as an object's property name?
This issue was raised in indirectly previous a stackoverflow question. the answer seemed general consensus by @Squeegy: You can use those ...
15
votes
3answers
304 views
What is this generated code supposed (intended) to do?
I saw this in an auto-generated javascript file:
function map(x){
x={x:x};
delete x.x;
return x
}
My conclusion is that is used to create an object, but why create it in that way? Is it ...
2
votes
5answers
549 views
How does a jQuery instance appear as an array when called in console.log?
When entered into a JavaScript console, a jQuery object appears as an array. However, it's still an instance of the jQuery object.
var j = jQuery();
=> []
console.log(j);
=> []
...
15
votes
6answers
3k views
chrome re-ordering object keys if numerics, is that normal/expected
i noticed that certain code that evaluates some shoe sizes JSON for an e-commerce site and outputs them on screen is messing up the order in chrome.
the JOSN string given can be:
...
8
votes
1answer
1k views
Advantage of using Object.create
Similar to, but different from this question. The code below is from JavaScript: The Definitive Guide. He's basically defining an inherit method that defers to Object.create if it exists, otherwise ...
7
votes
5answers
10k views
jQuery create object from form fields
How can I create a object with a form's fields and values?
like this one:
{
fields:
{
name: 'foo',
email: '[email protected]',
comment: 'wqeqwtwqtqwtqwet'
}
}
assuming ...
4
votes
2answers
436 views
Shorthand way to construct JS object with variable property name
Is there a shorthand way to create an object with a property field variable?
Say I have the variable PROP.Todo.PRIORITY = 'priority' and then, using Backbone in this example, I want to save this ...
4
votes
3answers
472 views
JavaScript “this” referce to wrong object
Well, this doesn't really refer to the wrong object, but I do not know how to refer to the correct one.
function someObj() {
this.someMethod1 = function() {
var elementBtn = ...
1
vote
2answers
102 views
B extends A, but B.add populates A.prototype.property
I have one class and another that inherits property children from the first one.
function A() {}
A.prototype.children = [];
function B() {}
B.prototype = new A();
B.prototype.addChild = ...
7
votes
2answers
157 views
javascript methods that can not be called from jquery objects?
I was reading Learning jQuery 1.3 jonathan chaffer and karl swedberg and while sorting table , they used .get() before calling .sort(), and said we need to transform jqurey objects into array of DOM ...
1
vote
1answer
536 views
Access JavaScript Object Literal value in same object [duplicate]
Possible Duplicate:
Self-references in object literal declarations
Is there any way access the value of a property name in the same object literal? Something like this:
myFunction.init({
...
0
votes
2answers
2k views
Dynamic Javascript Tree Structure
I would like to build the hierarchy dynamically with each node created as a layer/level in the hierarchy having its own array of nodes. THIS SHOULD FORM A TREE STRUCTURE.There should be a root node, ...
12
votes
2answers
4k views
How to set up JavaScript namespace and classes properly?
It seems there are so many ways to set up a JavaScript application so it is confusing as to which one is correct or best. Are there any difference to the below techniques or a better way of doing ...
10
votes
7answers
6k views
Javascript creating objects - multiple approaches, any differences?
I have seen a few different ways to instantiate objects in javascript, wanted to know the benefits/drawbacks of the various approaches and why you would use one over the other.
Approach 1
var obj = ...
6
votes
3answers
380 views
Javascript: why Object.keys(someobject), rather than someobject.keys?
I frequently get an array of an objects keys using:
Object.keys(someobject)
I'm comfortable doing this. I understand that Object is the Object constructor function, and keys() is a method of it, ...
5
votes
2answers
9k views
Length of a javascript associative array
I have a js associative array (or some may prefer to call it as an object) like, say
var quesArr=new Array();
quesArr["q101"]="Your name?";
quesArr["q102"]="Your age?";
quesArr["q103"]="Your ...
6
votes
4answers
4k views
JavaScript getter support in IE8
Check out this code. This is a very simple JavaScript object which is implemented using Module Pattern (and you can see the live example at this fiddle address)
var human = function() {
var ...
4
votes
1answer
861 views
is “Simple Javascript Inheritance” by John Resig still ok?
I've found http://ejohn.org/blog/simple-javascript-inheritance/ and it's exactly what I'm searching for but 'm wondering if it still works and if can cause any issue.
4
votes
4answers
348 views
Can I alias a key in an object literal? [duplicate]
Possible Duplicate:
Self-references in object literal declarations
I have an object literal which is used as a configuration element and is looked up for keys.
customRendering:{
...
3
votes
5answers
569 views
Mongoose/MongoDB result fields appear undefined in Javascript
Is there something that I'm missing that would allow item to log as an object with a parameter, but when I try to access that parameter, it's undefined?
What I've tried so far:
console.log(item) => ...
3
votes
1answer
3k views
From which version, IE can support Object.create(null)?
You can create an object in JavaScript in many ways:
// creates an object which makes the Object, prototype of data.
var data1 = new Object();
// Object literal notation; Object still is the ...
3
votes
1answer
1k views
JavaScript/JSON: Get unknown property of an object
If I have a JSON object like this:
{
"message": {
"name": { "stringLengthTooShort": "blub" }
}
}
The name of the property (here) stringLengthTooShort is changing every time,
how ...
2
votes
2answers
1k views
Whats the best way to remove a property from nested javascript Object?
I have a tree object as below, I am trying to remove the items array property if it's empty. I am not sure on the best approach to do this?
I am thinking of looping through the key, check the ...
2
votes
1answer
120 views
Trying to understand the new keyword
I'm trying to understand exactly what the javascript new keyword means, why it is necessary and when I should use it.
Consider the following examples:
var x = new function(){
var ...
1
vote
1answer
384 views
Better approach nulling variables, objects in javascript
I am building something for mobile and would like somehow to clear, null objects, variables to release a bit of memory. Here I have two quick examples, both are anonymous functions as I believe but ...
0
votes
5answers
266 views
Deleting a row from javascript object
I have a javascript object which looks like this :-
var myObject = [{"id": "1", "URL": "http://shsudhf.com", "value": "1"},
{"id": "2", "URL": "http://shsusadhf.com", "value": "2"},
...
5
votes
2answers
20k views
Pass object to javascript function
I have recently been messing around with jQuery on my website, and I have a fairly limited knowledge of Javascript. I am beginning to like the jQuery ability to pass variables to a jQuery function ...
3
votes
1answer
845 views
error with google swiffy calling runtime.js multiple times in the same page
I have converted multiple swf files using google swiffy v5.2 and will have my new animations displayed on many different pages, most of which I do not have control of or access to. In order for the ...
3
votes
2answers
782 views
How to Search and Manipulate Complex JavaScript Objects in a jQuery fashion
In the context of a web app, I have a server which sends or receives JSON strings based on the input from the client. On client consumption, these JSON strings are immediately converted into ...
3
votes
1answer
2k views
Using setTimeout() within a JavaScript class function
Is it possible to use setTimout() within a JavaScript object?
Currently the animation method call is running once, it seems that the setTimeout() isn't doing its job. I have managed to get it ...
3
votes
3answers
2k views
How to generate nested json object from nested list with javascript/jquery
I would like to generate the following object:
var ideaBoard = {
"Staff Retreat" : {
"Games" : [
{"title" : "Rockband", "details" : "1hr"},
{"title" : "Texas ...
2
votes
2answers
463 views
Performance concerns when storing data in large arrays with Javascript
I have a browser-based visualization app where there is a graph of data points, stored as an array of objects:
data = [
{x: 0.4612451, y: 1.0511} ,
... etc
]
This graph is being visualized ...
2
votes
1answer
292 views
Object nested property access
I'm trying to write a function that adds an accessor for each nested property in an object. To make this a bit clearer, given object o, and a string representing a path, I should be able to access the ...
2
votes
2answers
10k views
Get key value of dictionary by index in jQuery
I have a javascript dictionary object which has a pre-set keys that are defaulted to 0. Then I need to loop through the elements of this dictionary by index and use the value of the key to set its ...
2
votes
2answers
469 views
Javascript Literal Object Notation This vs Object Name
I have an object literal like this:
var test = {
one: function() {
},
two: function() {
this.one(); // call 1
test.one(); // call 2
}
};
What is the difference ...
2
votes
1answer
3k views
Error in accessor property: can't redefine non-configurable property 'status'
I'm trying to define an object and create an accessor property for it.
HTML:
<input type='hidden' id='crudMode' value='Create' />
JavaScript:
crudMode = {
create: "Create",
read: ...
2
votes
1answer
3k views
JSON Error when parsing “… has no method 'replace'”
Let me preface this with the admission that I am a complete programming and javascript noob and that fact is the source of my trouble.
I'm trying to populate a large array of custom objects from a ...
1
vote
3answers
103 views
Understanding the superior method introduced by Crockford
In the functional inheritance pattern, crockford introduces a new superior method via:
Object.method('superior', function (name) {
var that = this,
method = that[name];
return function ...
1
vote
2answers
832 views
Merging two backbone collection and models into one object using underscore
I have two backbone collections Categories and Items, Categories contains category models with id, name, and items (w/c contains item ids in string format separated by commas) and Items collection ...
1
vote
1answer
161 views
Reducing global variables and maintaining function flow
Javascript is not my first language of choice, nor is it one I am all that well versed in.
Functional stuff fine, objects, not so much.
I am looking to see if anyone can suggest options for dealing ...