The Most Disliked JavaScript Feature? (With Statement)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going guys my name is Dom and today we're going to be having a look at one of the most disliked features of JavaScript now most of you probably haven't heard of this feature right now and I'm going to start by saying that it is currently deprecated which means you should not use this for your projects whether it is for your personal project or your you know your real projects that are client-facing Etc you should probably avoid using this feature as it raises problems with confusion now I have heard like I said it is one of the most disliked features and I'm going to be showing you a code snippet right now that looks something like this as we can see we've got a constant called people which of course is an array of people Dom Alex and Sarah now we can see on line three we have this syntax right here it says with people console.log log length now some of you may know what this is doing based on how the code looks and what it says but for those of you who don't know this right here is perfectly valid code and we're going to be getting a results in the console let's run this script here we can say node index.js and press enter and we get three right there now of course you may have noticed that 3 is the length of the array of people and yes that is correct the length here refers to that length but what is actually happening here because you know when when it says console log length there is no variable or function called length so how is it getting this value well the way it works is in my own words is that when you say with people everything inside this block has people as as its primary scope now that definition there might not be 100 accurate but it is the way I understand this and how I like to think about it because essentially when JavaScript sees something like length as an example inside a function it first tries to look at the local scope are there any variables called length if there is it's going to use it otherwise it might go to the global scope to try and find the length a variable or property but in this case here it considers people as being part of that you know scenario part of where length might come from so essentially this code here is just saying people dot length so you can emit that first part people dots because you're saying with people give me the length now you can probably see why this is deprecated and why it is not recommended to go ahead and use this one because of course it can be confusing you're always trying to guess okay where is length coming from is it a variable is it part of people and essentially imagine you got a with statement with 30 40 lines long and you might have multiple if statements a couple of nested blocks it's going to get increasingly difficult to you know find out where your references are coming from because you're always in the back of your head you're always thinking well is that part of the people object in this case now of course people objects referring to the people array because in JavaScript almost everything is an object including the arrays now we've seen here how this works for properties let's talk about methods because the methods work in the exact same way let's update this code here I want to console.log people okay so I'll just run this code right now of course we get the array right there as it currently stands but I want to add a name to the array and then also remove one of them so I'm going to say here push then say Johnny then I'll say shift okay so now I expect Johnny to be added to the array and also using the shift method remove the first item in the array so we should get Alex Sarah and Johnny I'll run this code and of course we do indeed get Alex Sarah and Johnny because these methods here have ran and just like it was for the property of length this is just like saying people dot push and then people dot shift so the main takeaway of this video is that you should probably not use or you you definitely are not recommended to use the with statement in your JavaScript code but the main point is when you do see it being used hopefully not very often but if you do see this being used essentially you're emitting that first part where you say people dot and you go straight into the method call or the property reference and one last thing to mention is that when you call these methods of course the this keyword is going to refer to the object being caught on within the with statement just like it would normally if you were to say people dot push and that is all for today's video I hope you guys enjoyed that one and you learned something if you did make sure to drop a like And subscribe to the channel and I'll see you in the next video
Info
Channel: dcode
Views: 1,593
Rating: undefined out of 5
Keywords: javascript with statement, rare javascript tutorial, how to get better at javascript, javascript deprecated features, how to build javascript skills, how to be a better javascript developer, javascript tutorial, lesser known things about javascript, unique things about javascript
Id: clPc0z3nGq8
Channel Id: undefined
Length: 5min 41sec (341 seconds)
Published: Mon Jun 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.