Practical Uses for Prototypes with Arrays

  Рет қаралды 4,578

Steve Griffith - Prof3ssorSt3v3

Steve Griffith - Prof3ssorSt3v3

6 жыл бұрын

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.
Code GIST: gist.github.com/prof3ssorSt3v...

Пікірлер: 26
@ranjan_v
@ranjan_v Жыл бұрын
Thank you Steve
@chesterxp508
@chesterxp508 2 жыл бұрын
Another very cool tutorial !
@rmorabia
@rmorabia 6 жыл бұрын
Loved the Archer reference! Thanks for the video.
@zmark2274
@zmark2274 3 жыл бұрын
Oh, my goodness, it took me almost all evening to understand the idea)) Bravo!!! Stupid me)
@rotrose7531
@rotrose7531 4 жыл бұрын
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.
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 4 жыл бұрын
I have a whole playlist on loops if that helps.
@rotrose7531
@rotrose7531 4 жыл бұрын
@@SteveGriffith-Prof3ssorSt3v3 Thank you for the suggestion. I have found the playlist you pointed. Sure will repeat loop practice until I understand it.
@heartofrumi
@heartofrumi Жыл бұрын
great tutorial
@maitran1764
@maitran1764 4 жыл бұрын
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!
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 4 жыл бұрын
The concat method is a method belonging to String objects. So, you have to have a string in front of the method call "".concat( ) The stuff inside the parentheses is what you are adding to it. Sometimes I throw in extra things that are good to know about. Here you could use the concat method to add multiple things together. It also shows `this` being used in different ways, just to demonstrate that you could. You could just do this: let temp = this[i]; and let other = this[pos];
@jasbindarsingh1640
@jasbindarsingh1640 4 жыл бұрын
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?
@jasbindarsingh1640
@jasbindarsingh1640 4 жыл бұрын
we can override prototype props
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 4 жыл бұрын
The prototype methods are attached to the constructor of each object. If your object instance has a method by the same name as the one in the prototype then JS will call that one and never bother to look up the prototype chain. You will not have replaced or destroyed the prototype method, just stopped JS looking up the chain.
@maniac5411
@maniac5411 2 жыл бұрын
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?
@Felipekimst
@Felipekimst Жыл бұрын
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
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 Жыл бұрын
A name for an Array is what the variable is.
@Felipekimst
@Felipekimst Жыл бұрын
@@SteveGriffith-Prof3ssorSt3v3 lol I kinda found a way to do it but it looks messy and I don't really know what is going on grid = Object.create(Array.prototype, {limits: {configurable: true, writable: true, enumerable: true, value: {right: right, down: down, left: left, up: up}}} ) grid.push('hi') grid.pop(); it behaves like an array as expected and I can acess its limits property but if you set an index to the array, it doesnt increase the length property like: grid[0] = 'hi' the length is still 0
@kirisutokyoto3505
@kirisutokyoto3505 Жыл бұрын
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
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 Жыл бұрын
I didn't do anything about duplicates in this code but I have a video about that - kzbin.info/www/bejne/q4Wqg32no51rb7M
@akash_gupta_2090
@akash_gupta_2090 2 жыл бұрын
but u still changing the order of names also, how do we restrict that ??????
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 2 жыл бұрын
Do you mean how to create a new Array with the sorted values instead of making a destructive method that changes the original array?
@akash_gupta_2090
@akash_gupta_2090 2 жыл бұрын
@@SteveGriffith-Prof3ssorSt3v3 yes correct how ?
@Diamonddrake
@Diamonddrake 4 жыл бұрын
You made it only work on arrays of strings.
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 4 жыл бұрын
You can build your own methods to work on Arrays of whatever you want. It is an example of how to extend Arrays using their prototype.
@Diamonddrake
@Diamonddrake 4 жыл бұрын
But you extended the prototype of all arrays with a function that breaks on anything that’s not strings, not an ideal example
@AdrianSkar
@AdrianSkar 4 жыл бұрын
@@Diamonddrake this seems to work just fine if you need it for other types: let temp = this[i]; let other = this[pos]; this[i] = other; this[pos] = temp;
Creating a Read Progress Bar
14:17
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 1 М.
What is Factory Function in JavaScript? - JS Tutorial
17:31
ColorCode
Рет қаралды 56 М.
ВОДА В СОЛО
00:20
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 34 МЛН
Inside Out Babies (Inside Out Animation)
00:21
FASH
Рет қаралды 23 МЛН
Doing This Instead Of Studying.. 😳
00:12
Jojo Sim
Рет қаралды 14 МЛН
What is Prototype Chaining in JavaScript
15:33
procademy
Рет қаралды 8 М.
The Differences between ES6 Maps and Sets
6:09
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 7 М.
Deep Copying vs Shallow Copying
13:35
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 23 М.
Real World Array Method Uses in JavaScript Web Development
24:18
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 12 М.
JavaScript Array Sort Method Practice in 5 Minutes
7:13
James Q Quick
Рет қаралды 35 М.
Visually Understanding JavaScript Prototypes
14:58
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 53 М.
JavaScript Arrays Crash Course
6:04
Web Dev Simplified
Рет қаралды 100 М.
9.19: Prototypes in Javascript - p5.js Tutorial
19:24
The Coding Train
Рет қаралды 257 М.
ES6 Iterator & Generator Fundamentals
18:18
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 31 М.
ВОДА В СОЛО
00:20
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 34 МЛН