Array literal or string split? [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Lets say you needed a hardcoded list of strings (the question here is not whether or not you should hard-code things).
Is there a reason to prefer this:
var things = 'a b see'.split(' ');
over this:
var things = ['a', 'b', 'see'];

Pros of first approach:
Easier to refactor data from code. (String is not language specific)
Pros of second approach:
Less prone to error (what if your string has a space in it?)
Easier to format automatically (can and probably should separate each string by newline)
Easier to get data from alternate sources in the future.
Signals intent more clearly
Tiny bit more efficient

Related

arranage array from highest to lowest value [closed]

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 3 years ago.
Improve this question
I need to arrange an array from highest to lowest in a function
i have used array.sort but was informed that that was not allowed for my exerccise
function minmax(array){
var ar = array.sort().join();//I cant use array.join methood
return ar
}
Please help out
One solution could be to implement any of the popular sorting algorithms, but make the comparison used prioritize larger numbers. One example is this link, the code of which only needs a > flipped to a <.
Note that if this is a homework or school excercise, copying from anywhere is plagiarism and is generally discouraged.

Why does JavaScript have the Math object? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
In many languages, to find cosine, you use cos(x). But in JavaScript, you must use Math.cos(x). Why doesn't JavaScript spare us the 5 characters in Math., both making it easier to type and easier to read?
I have tried to Google this multiple times, and found no answers. Is there any practical reason for this that I have not yet found?
So far, there are three reasons I can think of:
The creators of JavaScript want to ensure that the math functions do not coincide with other functions users create (Like a function called 'cos()` that calculates, say, cosecant)
The creators of JavaScript thought that Math would make the code more readable
The creators of JavaScript perhaps didn't want any functions that have window as a parent (Though alert and prompt make this unlikely)
To hold the math functions without polluting the global namespace.

Why not Number.isNumber or Function.isFunction? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Although javascript is a weakly typed language, in some scenarios is needed to ensure the data type. Currently many node packages depend on helper functions, e.g, isNumber and isFunction.
It'd be interesting if there were built-in methods for that like Array.isArray. Something like Number.isNumber or Function.isFunction.
What do you guys think about this?
You should not worry much about such. There are already a lot of methods. For your query, you can use Number.isNaN for checking number and use typeof to know if the given variable is a function.
You can check the following answers:
Is there any function like IsNumeric in JavaScript to validate numbers?
How can I check if a javascript variable is function type?
Array is a collection. The collection might refer to [...] or {...} and this is why there's Array.isArray to check if the given collection is array or not. But there's no other way arround for differentiating the function and the number between themselves. And this is perfect reason the javascript should not have such function.

How to work with coding styles clashe within a single project across different languages? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
In Python underscores are used for variables:
some_long_var, my_name, first_name, etc.
In JavaScript camelcase is preferred:
someLongVar, myName, firstName, etc.
All is fine and dandy until the two technologies start interacting:
JS Ajax Call:
$.get('url', {foo_bar: fooBar, bar_baz: barBaz}).done(function (data) {
console.log(data.computed_result);
};
Python view:
def url():
foo_bar = request.GET.get('foo_bar')
bar_baz = request.GET.get('bar_baz')
return jsonify(computed_result=foo_bar + bar_baz)
Do I use the Python or the JS style within the data JSON object sent to the server?
What about the JSON response?
Pick one and be consistent. I write Python a lot, therefore I would choose the Python style, which is concerned with readability for non-native-English speakers. However, reasonable people could disagree. Build concensus on your team, and take what you can best agree to be the best course of action.

Can a JavaScript object be considered a plain dictionary? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Today I heard that a JavaScript object is no more than a plain dictionary (taking as a reference the python dictionary)
I think it's a wrong idea, but I did not know how to answer to that.
So, can you give me reasons why objects are not simple and dry dictionaries?
My first idea: prototypical inheritance.
...
Well, do dictionary items have an implicit this reference to the dictionary?
If not, then a Javascript object is a bit more than a plain dictionary.
As dystroy points out, this is created by using a function to create the object, so it is probably more of a closure around the object.
So in that case, yes, an object is probably very much a dictionary :/

Categories

Resources