Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I come across below statement in various JS code where first a function let's say "test" is declared thereafter used as mentioned below:
type: "test"
Please explain what does this statement mean where "test" is a function name.
Sample code below:
var MyFavoritesAjax = Class.create();
MyFavoritesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
ajaxFunction_getFavorites : function() {
var result = this.newItem("result");
result.setAttribute("message", "returning all favorites");
this._addFavorite("color", "blue");
this._addFavorite("beer", "lager");
this._addFavorite("pet", "dog");
},
_addFavorite : function(name, value) {
var favs = this.newItem("favorite");
favs.setAttribute("name", name);
favs.setAttribute("value", value);
},
type : "MyFavoritesAjax"
});
Now that you've shown the rest of the code:
type : "MyFavoritesAjax"
is a property declaration on an object being passed to Object.extendsObject(). It creates a property on that object named "type" and gives it a value of "MyFavoritesAjax".
This is similar to the other properties on that same object ajaxFunction_getFavorites and _addFavorite, though they have function references as their value instead of a string.
Given what you posted, you have a label statement (with the label type) including a simple expression statement consisting of the sole string literal "test". You could (and should) add a semicolon after it. However,
type: "text";
does not make much sense, it neither has any effects when executing nor does the label identify a loop.
Ah, your code example sheds some more light on this. As expected, you have an object literal that is passed to the Object.extendsObject function:
{
type : "MyFavoritesAjax"
}
Here, "type" is the property name and the "MyFavoritesAjax" string the property value. What this function does with it, and what it means in that contexts, should be documented with that function. I don't know it, it's not native.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a function that returns the following:
{ 'random_string': '' }
Where random_string is an id that I do not know until it is returned.
How do I extract the value of random_string in JS? Thank you.
var a = { 'random_string': '' }
console.log(Object.keys(a)[0])
Your variable a has a value of type Object. Take a look at the Object prototype's documentation and see which methods are available to you: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
You're trying to get your object's first key, and so conveniently you can use Object.keys as follows:
var a = { 'random_string': '' }
// Get array of Object's key values as strings
var aKeys = Object.keys(a);
console.log(aKeys);
// Print first key, in this case you only have one
console.log(aKeys[0]);
But based on your comments, you're going about this wrong.
If your "random_string" property identifier is truly random, you'd want to store the random string as an object property value.
That might look something like this:
// Generate some random string value
var random = Math.random().toString(36).substring(7);
// Create an object with a predefined object property identifier
var data = { 'random_value': random };
// Access the random value using your known property identifier
console.log(data.random_value);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I was taking a challenge and one of the questions seems like I got the right answer but it wouldn't pass. Need help understanding why it didn't.
Challenge: Add a method to the Person's prototype called "shoutName" that returns the person's name in all uppercase letters.
function Person(name) {
this.name = name;
this.shoutName = function() {
name.toUpperCase();
return '"' + name.toUpperCase()+'"'
}
}
/* Do not modify the code below this line */
const john = new Person('John');
console.log(john.shoutName(), '<-- should be "JOHN" ');
The question said to add a function to the constructor's prototype.
You didn't do that. You modified the constructor to dynamically add the function to the instance as the instance was created.
Person.prototype.shoutName = function () {
return this.name.toUpperCase();
}
Your function also wrapped the resulting value in quotes, which the question didn't ask you to do.
From your tiny picture, I noticed that your code was:
return '"' + name.toUpperCase() + '"';
Not sure why you added the quotes, just return this.name.toUpperCase(); and it should work fine. You should be referencing this object's property, rather than the input value of just name.
Also, having name.toUpperCase(); on a line by itself does nothing. Unnecessary calculations since that function returns a value that you're not assigning.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Each ArrayItem contains no. of Property like Id, Name, Description etc
But we want to Fetch ArrayItem with Help of Name Property .
So Please give me code suggestion in Jquery or backbonejs without using for loop.
If you are using BackboneJS, you already have UnderscoreJS installed. Underscore has several methods for searching through a collection. For instance, using _.findWhere(...):
var myArray = [ ... ];
var helpItem = _.findWhere(myArray, { name: 'help' });
This would return the first entry in the array that has a name property equal to 'help'. _.findWhere(...) can match multiple properties too. If you want to find an item with something other than direct equality of properties, you can use _.find(...):
var overTwentyOne = _.find(myArray, function(entry) {
return entry.age > 21;
});
This would return the first entry in the array whose age property was greater than 21.
Note also that most of the list-centric methods in Underscore are automatically mixed into all Backbone.Collection instances. So if you were searching through a Collection, the above findWhere(...) example could be more simply written as:
var helpItem = collection.findWhere({ name: 'help'});
This would return the first Backbone.Model instance from collection that had a property name equal to help.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to access a list of Casino Games and their properties from a JSON object in order to get them to display on the website. This is where I am so far.
var x = $.getJSON("http://api.bosurl.net/V1/Progressive.asmx/GetProgressiveGames?format=json&callback=?", ProcessProgressiveGames);
console.log(x);
function ProcessProgressiveGames(progressiveGames) {
console.log(progressiveGames.d.Games[0].GameName);
}
What's there best way to do this. If you check your console, you'll see that var x contains the object with the game data.
See also: http://pasteboard.co/kXb1Voq.png
Related fiddle: http://jsfiddle.net/emporio/vz24dtm8/5/
This question is unique because it requires accessing unique properties. The answers also provide a multitude of ways to retrieve the data.
DEMO
JS
var Games;
var x = $.getJSON("http://api.bosurl.net/V1/Progressive.asmx/GetProgressiveGames?format=json&callback=?", function (data) {
Games = data.d;
}).done(function () {
document.getElementById('choice').innerHTML=ProcessProgressiveGames(Games);
});
function ProcessProgressiveGames(progressiveGames) {
return progressiveGames.Games[0].GameName;
}
$.getJSON function returns promise and the proper way to handle data returned from this address is to set second argument - function to handle data
$.getJSON("http://api.bosurl.net/V1/Progressive.asmx/GetProgressiveGames?format=json&callback=?", ProcessProgressiveGames);
function ProcessProgressiveGames(progressiveGames) {
// Note that progressiveGames has a property 'd' containing the data we're interested in.
console.log(progressiveGames);
document.getElementById('choice').innerHTML = ProcessProgressiveGames(x);
return progressiveGames.d.Games[0].GameName;
}
http://jsfiddle.net/vz24dtm8/7/
You can try like this
var x = $.getJSON("http://api.bosurl.net/V1/Progressive.asmx/GetProgressiveGames?format=json&callback=?", ProcessProgressiveGames);
http://jsfiddle.net/vz24dtm8/8/
function ProcessProgressiveGames(data) {
// Note that progressiveGames has a property 'd' containing the data we're interested in.
return document.getElementById('choice').innerHTML=data.d.Games[0].GameName;
}
<pre>Use callback function to get the json value and pass the parameter in callback function</pre>
http://jsfiddle.net/rajen_ranjith/vz24dtm8/13/
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Note: This is a continuation of another question that I decided were two separate issues that need to be solved. I'm also currently not sure of how exactly to phrase this question, so I will try my best and when I get more clarity I will rephrase my question for future reference.
I'm writing two basic jQuery plugins, $.fn.query and $.fn.build, that sort an array, and create html code to insert into a document, respectively. I'm currently testing it with Vimeo video ID's that I will display videos with.
$.fn.build has three parts. First it wraps every array item with individual containers, the builds them into rows (problem area), then lastly it wraps everything in a container. (every part of this is optional).
Specifically the problem comes from this line: $(tmp).add(newRow); although it is valid javascript.
if ( options.splitBy !== undefined && options.wrapRow !== undefined ) {
var tmp = $([]),
newRow = function(i) {
$(build.splice( i, i + options.splitBy )).wrapAll( options.wrapRow ).parent();
};
for (var i = 0, l = build.length, a = options.splitBy; i < l; i += a) {
$(tmp).add(newRow);
}
build = tmp;
console.log(build);
}
See: http://jsbin.com/upatus/2/edit
I am quite sure that you want to use the function, instead of adding the function itself. Also, you will want to use the same tmp object all over the time, instead of wrapping it into a new jQuery instance and not adding to the original one. Try
tmp.add(newRow(i));
BTW: If you want to build an array, you should use
var tmp = [];
and
tmp.push(…);
Now I've looked at the code from the other question. Both answers are correct, and contain some valid points:
splice is an Array function on jQuery's prototype, and returns an array. (You have fiexd this now)
Your query method returns an array, but should return a jQuery instance for chaining
Your build variable was not initialized, but used
You should really choose whether you want to use arrays or jQuery objects internally in your function, and not mix them.
BTW, you should rename your functions to more descriptive names. "build" and "query" are very vague and may collide with other plugins.