Implement a custom forEach in javascript | JS Interviews
HTML-код
- Опубликовано: 10 сен 2024
- Visit chaicode.com for all related materials, community help, source code etc.
Javascript interview series
Sara code yaha milta h
github.com/hit...
Discord pe yaha paaye jaate h:
""/discord
Instagram pe yaha paaye jaate h:
/ hiteshchoudharyofficial
Small correction. It’s actually this.hasOwnProperty(i)
I missed it while explaining.
i think it's actually this.hasOwnProerty(this[i]) cause "i" is just index isnot it ?
this.hasOwnProperty(this[I])
@@Error404Codes this.hasOwnProperty(i) is the correct way, because incase of array, hasOwnProperty() method checks whether an index exists or not.
Second implementation was unexpected, thank you sir for making it on more advanced level.
Glad you liked it!
This JavaScript code is extending the built-in Array.prototype with a new method called forEachTwo. This custom forEachTwo method is designed to iterate over an array and apply a provided callback function to each element. Let's break down the code step by step:
Extending Array Prototype:
The code begins by defining a new method called forEachTwo on the Array.prototype. This means that once this code is executed, all arrays in the JavaScript environment will have access to the forEachTwo method.
Function Parameters:
The forEachTwo method takes two parameters:
callback: This is the function that will be called for each element in the array.
thiscontext: An optional parameter that represents the context in which the callback function should be executed.
Callback Function Check:
The code starts by checking if the provided callback is a function. If it's not a function, an error is thrown with the message "Not a function."
Getting Array Length:
The length of the array on which the method is called is stored in the length variable.
Looping through Array Elements:
The code uses a while loop to iterate over the array's elements. The loop continues as long as the index i is less than the array's length.
Checking Property Ownership:
Inside the loop, there's a condition if (this.hasOwnProperty). However, this condition is incomplete and should actually be checking if the array instance itself (this) has a property corresponding to the current index i. The correct condition should be if (this.hasOwnProperty(i)).
Executing the Callback:
If the array has the property at the current index, the callback function is executed using the call method. The call method allows you to invoke a function in a specific context (thiscontext) and pass arguments to it. The callback is invoked with three arguments:
The current array element (this[i])
The index of the current element (i)
The entire array (this)
Incrementing the Index:
After executing the callback (if applicable), the index i is incremented by one.
The purpose of this code is to create a custom version of the forEach method for arrays, which can handle a specified execution context for the callback function. However, there is a minor issue in the code related to the if (this.hasOwnProperty) condition that needs to be corrected for the code to work as intended.
This JavaScript code is extending the built-in Array.prototype with a new method called forEachTwo. This custom forEachTwo method is designed to iterate over an array and apply a provided callback function to each element. Let's break down the code step by step:
*Extending Array Prototype:*
The code begins by defining a new method called forEachTwo on the Array.prototype. This means that once this code is executed, all arrays in the JavaScript environment will have access to the forEachTwo method.
*Function Parameters:*
The forEachTwo method takes two parameters:
*callback:* This is the function that will be called for each element in the array.
thiscontext: An optional parameter that represents the context in which the callback function should be executed.
*Callback Function Check:*
The code starts by checking if the provided callback is a function. If it's not a function, an error is thrown with the message "Not a function."
*Getting Array Length:*
The length of the array on which the method is called is stored in the length variable.
*Looping through Array Elements:*
The code uses a while loop to iterate over the array's elements. The loop continues as long as the index i is less than the array's length.
*Checking Property Ownership:*
Inside the loop, there's a condition if (this.hasOwnProperty). However, this condition is incomplete and should actually be checking if the array instance itself (this) has a property corresponding to the current index i. The correct condition should be if (this.hasOwnProperty(i)).
*Executing the Callback:*
If the array has the property at the current index, the callback function is executed using the call method. The call method allows you to invoke a function in a specific context (thiscontext) and pass arguments to it. The callback is invoked with three arguments:
The current array element (this[i])
The index of the current element (i)
The entire array (this)
Incrementing the Index:
After executing the callback (if applicable), the index i is incremented by one.
The purpose of this code is to create a custom version of the forEach method for arrays, which can handle a specified execution context for the callback function. However, there is a minor issue in the code related to the if (this.hasOwnProperty) condition that needs to be corrected for the code to work as intended.
This video will compel you to think about how things work internally. Undoubtedly, this gives a better understanding. Thank you. I ended up creating custom map, filter, reduce methods :)
you are changing the way we code or the way we learn things
great efforts by you
we appreciate your effort
always be happy sir
ap hamesha salamat rahay
in depth explanation king
big respect KIng
Sir You're making a huge impact . Universe of JavaScript. Thank You Sir
Sir , please try to upload next react video soon..🙏
Aaj hi evening me kr dete h ji
@@chaiaurcode one of the best teacher and humble person , i have encountered in my life!!..🙏❤️
@@chaiaurcodeyes sir roz upload kiya karo
@chaiaurcode Thank you very much sir❤❤
Why you didn't get me earlier 😮 what a teacher you are
I wanted to express my heartfelt appreciation for your exceptional dedication in providing interview-focused content that delves deep into JavaScript concepts. Your effort in preparing us for real-world challenges is truly commendable, and I'm incredibly grateful for the valuable insights you've imparted to help us succeed in our future endeavors.
Sir just a small request,
aap batate ho ki, interview me question pucha karo,
question puchne se interview intrective rahta hai, interview acha jata hai,
Can you please video on that?
Kaise technical discussion karna hai?
kaise question kar sakte hai?
question karne ke time par kya kya chize dhyan me rakhni chahiye?
Aap ki communication skills badhiya hai sirji 🔥
Great series sir, learning a lot.
Concept pura refresh and ache se clear ho jata hai.
loved it hitesh sir ❤aur videos chiye jaldi jaldi
wow Sir amazing❤. I just learn portotype from this video alone.
mind blowing implementation of call with context
.........Wow😮, what a video.
Please make more such sensible and thinkable videos
Started loving your content and loving your way of talking even more
please continue this series
this series are very helpful for inteview
🚀Inside the loop, there's a condition if (this.hasOwnProperty). However, this condition is incomplete and should actually be checking if the array instance itself (this) has a property corresponding to the current index i. The correct condition should be if (this.hasOwnProperty(i)).
Ohh yes, absolutely correct. I missed it while explaining.
Thanks for the Interview series Sir , Much gratitude
Kaafi OP channel hai aapka!
Ye to h ji 😁
@@chaiaurcode aur kamal ki baat h free bhi hai 🤣😅
dhanyavad guruji
thanku so much sir for this amazing series😊😊
Thank you so much sir 😊
This series is amazing but React js series is OP ✅
Awesome Javascript questions!! 👏👏
Glad you like them!
Thank you sir.... Very helpful ❤❤
Dhanyawad Guruji...❤
wow !!!
sir ye topic map() method -pe bhi banao aur forEach and isko compare krke batao behind the scenes
sir ek requeest hay ap sy
kindly JAMSTACK vs MERNSTACK py ek video bana dijiye
❤❤❤❤❤❤
Amezing
@chaiaurcode, but I really don't understand why need a `hasOwnProperty` check, when I am iterating using `for` loop, I tried by adding custom properties on Array prototypes, it doesn't come in the picture, also we don't need if we use `for-of` loop. I understand we need it in `for-in` loop, but rarely anyone uses `for-in` to iterate over an array. I may be wrong, can you please correct me on this sir.
Also, I remember you've mentioned `hasOwnProperty` is very expensive operation, in the scenario of holey arrays.
Pranam Gurudev 🙏❤️
❤❤❤❤
thiscontext is undefined ??
How will we call this function forEachTwo ??
I'm little confused ?
best videos
Thanks
love form the core of heart❤❤
waiting for next react lecture videos
Very Informative 👍
Sir non cs walo ko job developer job milna muskil hai kya
sir please make more videos on fetch, promises, async/await their relations and usecases on real life and combine projects plz.......
Sir please continue this series
🎉🎉🎉 thanks sir
interesting
Awesome
Mubarak Ho Sir India ne chand par land kia hai ❤❤
Let's go
Samaj gaye Guruji...❤
Sir please javascript ke or interview questions bhi lao
Great video sir ❤
Nice video sir.
Han ji
I found it confusing that inside the while loop the condition used in `if` block is this.hasOwnProperty which i believe is a method not a property or am i mistaken?
You are correct. Slight mistake from my side
Amazing ❤
Writing for react😊😊
sir kindly explain me this
const ubit={
name:"akram",
seat_no:"eb21103066",
session: askSession=()=>{
console.log(`my session is ${this.session}. am i a good student?:${this.isgood}`);
}
}
const my=Object.create(ubit);
my.session="evening";
my.isgood="true";
my.askSession();
console.log(ubit);
why i can not use arrow function in an object for acacessing current context using this.
Sir, I had articles on custom forEach, map, filter at hashnode last year
sir event loop bhi bata do plz
🙏🙏
hum aap ko 500 likes den gay or aap hum ko 5 LPA ka package legwa dena , Kuch oor esay hi video ke sahyetaa se !!
:)
JavaScript Interviews ke liye best resource hoga ye
sir java script ka project ka ak playlist bna dijiye please
hey bro this playlist available in English ?
Not yet, but will be available soon
prototype?
ara ma na ap ki video next js ki freecodecamp pr dekhi thi ap hindi ma nextjs ki playlist bnai
What is thiscontext
Ok
salam ritesh bai.....typescript urdu mein bna dein....
from pakistan
sir java script ka project ka ak playlist bna dijiye jisme javascript ka only project ho please pl please
500 likes
100 comments
first like
First view 😊
🎉
i am not watching this video because you are using hindi instead of english
"use strict";
const freezeObj={
name:"akram",
password:2334,
};
Object.freeze(freezeObj);
freezeObj.name="jawad";
console.log(freezeObj.name);
sir why it is not giving me typerror
*Sir Rate this code*
Array.prototype.myForEach = function(callback) {
if (this == null) {
throw new TypeError('Array.prototype.myForEach called on null or undefined');
}
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
// Convert this (which should be an array) into an object to work with it
let Obj = Object(this);
// Get the length of the array
let len = Obj.length >>> 0;
for (let i = 0; i < len; i++) {
if (i in Obj) {
callback(Obj[i], i, O);
}
}
};
❤
❤❤❤❤