This question already has answers here:
call and apply in javascript [duplicate]
(2 answers)
Closed 8 years ago.
I know the call and apply in javascript but how does exactly the difference between javascript Call and Apply..?? and another thing i found some code use this together like this:
function doSomething() {
return Function.prototype.call.apply(Array.prototype.slice, arguments);
}
are is the same as..
Array.prototype.slice.apply(arguments)
Why we want to use call and apply together?
No, it is not the same. Array.prototype.slice.apply(arguments) applies the slice function on the current argument object, while Function.prototype.call.apply(Array.prototype.slice, arguments); calls the slice function on the array which is provided as the first argument.
Maybe things like this will become easier with new EcmaScript syntax. Your doSomething is equivalent to
function doSomething(array, ...)
array.slice(...); // assuming array is really an array
}
whereas the second is equivalent to
function (...) {
arguments.slice(); // assuming argument objects are actual arrays
}
ase the same
Array.prototype.slice.apply(arguments)
yes, i think its the same too..!!
Related
This question already has answers here:
Get a function's arity
(5 answers)
Closed 3 years ago.
When using a 3rd party library I often find an optional parameter in the callback.
For example, in Mocha when the callback done parameter exists, it waits for done to be invoked before moving on to another test case.
someFunction(function(done) { /** code **/ })
someFunction(function() { /** this behaves differently than above **/ })
How can I achieve the same behavior?
You can check the length attribute of the function object
console.log((()=>42).length); // 0
console.log(((x,y)=>42).length); // 2
note however that you cannot be sure of exactly how many the function is going to access, because it's also possible to use arguments inside Javascript non-arrow functions and "rest" parameters inside arrows (that are not counted in .length attribute).
This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 4 years ago.
I was doing a question on codesignal and when I couldn't get the answer myself for all tests, revealed the answers and saw this one; while I understand what it's trying to do (make sure positions are not divisible by n, and if it is increment n by one) I'm having trouble understanding the arrow function syntax and, rewriting it myself from their code.
function obst(inputArray) {
for (var n=1;;n+=1) if(inputArray.every(x=>x%n)) return n;}
In Javascript, every function written like this:
function(args) {
// DO SOME STUFF
}
can be written like this:
(args) => {// DO SOME STUFF}
In your case, the method .every() expects a function, and
function(x) {
return x%n;
}
is written as
x => x%n
inputArray.every(x=>x%n)
is the same as
inputArray.every(function (x){
return x%n
}))
( except for the way the 'this' keyword works )
For any doubt about Javascript language, i recommend the You Dont Know Js series
written by Kyle Simpson.
It's a very enlightening book.
Tip: When you write a code, ask question to youself like:
what i'm doing?
assigment means equality?
what are the methods that can i use in string variables?
And something like that.
Stimulate yourself to know what in the actual fudge, you are doing.
Cheers!.
This question already has answers here:
Add method to string class
(6 answers)
Closed 4 years ago.
So, for learning purposes I'd like to recreate the "charAt()" existing method in Javascript which tells you the position of a character in a given string. I'll call the method "CharAtX"
To do so, I've created a function with 2 parameters : The first one is the word, the second is the position, here is the code I have :
function charAtX(word,pos) {
word_split = word.split("");
return(word_split[pos])
}
console.log(charAtX("Truck",2))
So, it obviously works, if i call charAtX("truck",2), i will have "u" returned.
But my question is the following :
The original charAt can be called like such
my_word.charAt(3)
Mine can't though. Why is that and how could I change my function into a method so that I can?
You have to call charAtx function in the context of String object.So, When you call string.charAtx, this object refers to the string. You have to learn prototype and this object in jvascript.
String.prototype.charAtX = function(pos) {
word_split = this.split("");
return(word_split[pos])
}
This question already has answers here:
Loop (for each) over an array in JavaScript
(40 answers)
TypeError: <Array>.each is not a function
(6 answers)
Closed 5 years ago.
I want to call each() on the resulting array of another function that I call.
Why can't I do:
callMethodThatReturnsAnArray().each(function () {
...
});
I get a callMethodThatReturnsAnArray(...).each is not a function error.
each() is jQuery for DOM elements - what are you doing exactly? For plain Javascript, you'll want to use forEach() or map()
Array has forEach method not each. Use
callMethodThatReturnsAnArray().forEach(function(entry) {
// ...do whatever you want to do
});
If you want to use each, Jquery has method
jQuery.each(array, callback )
So you can write code something like.
$.each(callMethodThatReturnsAnArray(), function(index, value){// do whatever you want to do.});
This question already has answers here:
What's the difference between this.bla to Object.prototype.bla
(4 answers)
Closed 9 years ago.
I want to know what is the difference between insideFn and outsideFn in the follwoing case :
function Construct()
{
this.insideFn = function(obj){
....
}
}
Construct.prototype.outsideFn = function(obj){
...
}
and which one is efficient to use ?
In the first case every instance created with Construct will have its own insideFn, which will waste memory in case you will use more than one instance. In the second case each instance of Construct will have just a reference to outsideFn. So the latter is better.
In the first construct the method is an attribute of the instance of Construct.
In the second construct the method is an attribute of the prototype object from Construct
If you define functions inside the prototype object, they will be the same when calling.
The prototypevariant often saves memory and speeds up the code.
You can also overload a prototype function within the instance of an object, to change the default behaviour of your object.
For better english and much more details see http://www.javascriptenlightenment.com/
The first one function Construct() {} is a class(in javascript it is an object) whereas later on you are inheriting(extending) this class using the keyword prototype and adding one more function outsideFn. There are no specific classes in JavaScript so you have to use objects as classes.