Practical Uses for Prototypes with Arrays

preview_player
Показать описание
While the prototype object can sometimes seem like a distant concept that has no real application to the code you are writing, here is a practical example of how you can use it to your advantage.

You can take existing objects and add new functionality by adding properties or methods to their prototypes.

This example shows how to extend the Array object but you can apply this same technique to Arrays, Dates, or anything you want.
However, it is recommended that you don't extend any of the DOM objects as it could adversely affect performance.

Рекомендации по теме
Комментарии
Автор

Loved the Archer reference! Thanks for the video.

rmorabia
Автор

Oh, my goodness, it took me almost all evening to understand the idea)) Bravo!!! Stupid me)

zmark
Автор

Thank you for the exellent explanation about array prototype. I need to learn something more to follow what is going on inside the for block, but the concept you explains is clear enough to help me do some practice. Thank you.

rotrose
Автор

Great video, thank you very much.

I understand the logic behind this but I'm quite confused about the part where you concatenated this[i] and this[pos] into an empty string. What do you mean by "taking the new value"? What would happen if I just skipped the empty string part? I'm new to manipulating strings so a clearer explanation would help a lot. Thank you again, you're the best teacher!

maitran
Автор

Great video Steve, I have two questions,
What if I wanted to shuffle the original array passed?

Can we override any pre-existing prototype method?

jasbindarsingh
Автор

Hey Steve, Thanks a ton for this video. I've two questions, if you'd kind to take out some time, and answer them briefly:
1. Inside the shuffle function, doesn't "this" point to original array? Based on that, it should be updating the original array!! {Clearly this is not happening so it brings me to my 2nd question}
2. How to create shuffle function using prototype to update the original array?

maniac
Автор

hello great video
i don't understand, there is a chance that the math.random will give you the same number twice how did u sort that problem in this code

kirisutokyoto
Автор

Hi, Steve! Thanks a lot! I was looking if I there was a possibilty to create a property to some specific arrays, and also borrow the methods from the array prototype

function ARR(name, ...items){
this.name = name;
this.array = items

Object.setPrototypeOf(this, Object.assign({name: this.name}, 'Array.prototype);

return this.array
}

var arr = new ARR('friends', 'john', 'mario', 'luigi')
it would have to return the array inside the function to use its methods
arr.forEach((a, i)=>{ console.log(a.name, i, a)})
// friends, 0, john
//friends, 1, mario
// friends, 2, luigi

does this sound too bizarre? lol It would really be convinient to be able to name an array

Felipekimst
Автор

but u still changing the order of names also, how do we restrict that

akash_gupta_
Автор

You made it only work on arrays of strings.

Diamonddrake