Which JS concept can ensure the code runs? [closed] - javascript

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
Can someone please help me understand what concept from JavaScript could ensure below line of code runs? feel free to make code changes to make it execute & provide output.
(20).add(10).multiply(2)

You can make use of prototype. Using .prototype, you are adding a property in the prototype chain to all numbers.
Any number can use it.
Number.prototype.add = function(x){
return this + x;
}
console.log((20).add(10));
Number.prototype.multiply = function(x){
return this * x;
}
console.log((20).multiply(10));
console.log((20).add(10).multiply(2));
Note: This is generally a bad practice, and should only be used for self-learning and practice.

Related

Pretty Basic question but why do you need brackets after defining a function? [closed]

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 2 years ago.
Improve this question
function exampleFunction() {}
Why do you need the () after defining exampleFunction??
The brackets at the end of a function can be used to define parameters when calling it.
For example:
alertSentence("Hello, this is an example!");
function alertSentence(string) {
alert(string);
}
This would show an alert dialog with the string: "Hello, this is an example!"
Javascript is a language that was defined following the patterns of c. Wikipedia says it's part of the simula programming language family link. Languages in this family define function parameters using parentheses. Other language families have other ways (see python for an example). Ultimately it's a bit of arbitrary syntax that allows programmers to explicitly define what they want the computer to do and a way for the computer to understand them.

Javascript: Renaming built in functions [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 Javascript, is it acceptable to rename built in functions. For example, in my code, I use document.querySelector() tons of times. This is extremely long and tedious to type every single time (even with autocomplete from the IDE's side). Therefore, I decided to create a new function with a shorter name like the following:
let qs = selector => document.querySelector(selector);
Is this an acceptable practice in the JS community? Is this considered bad code? If so, why? Additionally, if doing this is alright, what are some drawbacks?
Thanks.
No.
Someone is going to come behind you to edit your code.
They will then have to track down your renaming function to actually see what it does.
Create an snippet in your IDE if it’s that much of an issue for you.

How to create entites in javascript [closed]

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 8 years ago.
Improve this question
Simple question maybe, but I'm a bit lost here.
Been googling around for a while, but I can't find any information.
Maybe I'm seaching for wrong information..
So please:
A quick example-code would be great!
How do I create an entity in javascript holdning attributes??
I think the term you're looking for is 'object'.
var myObj = {
property : 'value',
otherProperty : 'otherValue'
}

What are these javascript methods [closed]

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 8 years ago.
Improve this question
Looking through some open source javascript library and I've come across some single character methods a few times:
myVar.c('foo').t('bar');
myVar refers to an XML DOM element. So what are c() and t()? I don't see any reference to such methods in the API: http://www.w3schools.com/dom/dom_element.asp
There is one remote possibility. When javascript is minified single letters are used to reduce function names. Your example above may be a minified version [file].min.js and therefore function names are obfuscated.

For/In Loop in java script [closed]

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 9 years ago.
Improve this question
I could understand what logic is going behind the condition inside the for loop but still i could not get whats going behind the screens. Is there any set of documentations to learn what are all the ways of putting parameters for a for loop !! ?
function getFieldNames(row) {
var FieldNames = []
for (var column in row) {
if (!row.hasOwnProperty(column))
continue;
FieldNames.push(column);
}
return FieldNames
}
Thank You .!
W3schools has documentation, and you can learn and test your code on the site.:
http://www.w3schools.com/js/js_loop_for.asp
Mozilla offers a more explicit defintion and gives examples:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
Here is an article that goes into a lot of detail and might be helpful:
http://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/
EDIT: I do see w3schools has some bad formatting and practices, but the general explanation of loops is correct, and easy for someone starting out to understand.

Categories