02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Don't object, it's only an object

Objects are natural to our life. If you step away from your computer for a second, you'll notice that everything that we can see is a type of object. In programming - in many ways -objects are exactly the same. Objects contain functions and variables that together construct an object.

When you think of it in the real world, everything is built out of smaller particles as well. The core building block of almost everything in programming is the primitive data types and functions we met. In this lecture we're going to take a look at some of these prebuilt objects and see what we could do with them.

Video Transcript

00:02Cats, they're a sort of object
00:06Object, there are a lot of different objects in the world. You know, this is my one object my bottle of water
00:12that I drink from this is
00:14like a beanbag point is is that objects in
00:18Programming are very similar to real tangible objects in the world. The idea is basically just a pile of things that are usable
00:26When with that really non-traditional intro
00:31Let's see what objects are in action
00:33into our source files and
00:36The first thing I want to do is I want to talk about
00:38Objects what they are. So we said already we've seen functions. We've seen variables
00:45but really
00:47imagine like every every programming language including JavaScript has hundreds of functions has hundreds of of
00:54variables from the most simple idea if we have to search from within this huge library of assets
01:00We'd have to create really big names and they wouldn't be organized in a very convenient way
01:06so the first thing that objects are before anything else is a really great mechanism to organize your functions and variables and just
01:14Put them into shelves in many ways or buckets
01:18No, let's just look at one example. So for example, the math is
01:39a
01:40You could say a static object in which basically means that we never create new math objects
01:47we're going to look at how you create new things shortly and
01:50The math object contains a lot of functions that will end variables that will help us do things that relate to the mathematics
01:57If it's math.pi, so I could go ahead and just
02:01Console log and just log out the pi value
02:05So if I save this and I go back into my browser and click on refresh
02:09We're gonna see that we're gonna get the value of pi
02:12Same applies if we go ahead and again just gonna call that console just so we could see the output of what comes out
02:19I could go ahead to the math library
02:21Click on dot and after the dot add for example random which we've seen that before which is a function and the random function
02:28Should return a value between 0 and 1
02:31So we've seen already and we worked already with objects in the idea that objects are basically a way to
02:38Organize all of our information and keep it in a separate place for us to have direct access to it
02:45Now we've just met the static although we've seen it before we met the static
02:50cluster of functions and variables
02:53Are there to help us do mathematical things and there's plenty of mathematical functions. I'll just give you one more example
03:16So one more example would be come again
03:19We're just gonna copy again to go back in and output what we're creating and instead of calling the PI or random
03:26I'll call the
03:28Let's call it max and the max function would I could send inside of the variables and it will output the biggest number
03:35So if I save this and I come back to my browser and I click on refresh
03:38We'll see that the largest number which was 100 would output now. We're not gonna go through
03:44Most of the built-in functions in JavaScript and that's really gonna be up to you
03:49To familiarize yourself with them as you develop things. You're gonna find that they're needed
03:54So my biggest recommendation to you after you're done with this course if you want to strengthen your capabilities in JavaScript
04:01It's just get a referral book or just go to an online site
04:05That just has a list of all the functions and just read about them to get familiar with those
04:10Capabilities because no one course could ever cover all the variety of things
04:15You don't have to go into every single item, but we could do is just find out what?
04:20What objects exist so then you'll know? Oh, okay
04:23They're if I'll need to ever something that relates to math before I try to figure out how to create it on my own
04:28I'll just search up and see what if that capability exists already pre-built into the language itself
04:34So that's it about about
04:36static
04:37Objects we've met them many times before and and just to repeat you see we said we have an object
04:43It could contain variables such as the PI variable
04:46it could contain functions and
04:49Definitely could contain functions that have properties as we've met in chapter 3 when we saw her how to work with functions that have
04:56Proper parameters, so I'm just gonna comment this out so we can move on to the next topic and the next topic that I want
05:03To talk about and I want to stay in this video still because we're still talking about the built-in
05:09Objects now, it's really nice to be able to approach the math object
05:13But one of the biggest features and capabilities the JavaScript is capability to interact with the things that are on our display
05:22Now the old way of doing things in JavaScript was quite complicated
05:27And I'm not gonna even talk about it because we're living in the world of HTML 5
05:31We're living in the world of jQuery, which makes life way easier
05:35So we're not gonna even worry about how do you select something that doesn't have an ID because you just will give it an ID
05:42Or how do you select something if you're not using attributes?
05:46But what I want to show you is a built-in function and by the way also a built-in object
06:08So there's a built-in object that's called document and let me put all let me put that underneath the
06:14The last thing we worked on so it's called document
06:17We've seen it already before notice how we're taking our document here
06:20And this is the first line we've ever created or almost the first line we've ever created and we sent it to jQuery to start up
06:27our application
06:28But we could address it directly and there's built-in functions such as the get
06:33Element by ID, which is a function that basically asks me to put inside an ID
06:39So for example my my h2 which I've previously created for you guys
06:44And what it will do is it will return that object to me as an object
06:50so if I if I go to my console and I log the value that's coming back and
06:57We save this. Oopsie. We save this when we go back to our browser and click on refresh
07:01We're gonna see that I'm getting that HTML element come back and it's even you see how it's highlighting in my jQuery
07:08Sorry in my browser with my firebug. So what I want to do is I want to show you just another something about that
07:15So we sort of have that HTML is coming back
07:17But I could literally save it as a variable and just call it my h2
07:22let's say for for example, and I could just save the value that comes back from my get element by ID and
07:31There we go
07:31And now I have that as a variable which I could then refer to and work with throughout my you know
07:37If I want to do a couple of things to it
07:38For example, what one thing that I might want to do
07:41I'm just gonna paste it just to show you that it's the same thing that my variable will contain that element
07:46If I go in to refresh again, you'll see that that element is gonna come back
07:50Its value is gonna come back again
07:52Now I just want to show you an example of one thing that we could do with it
07:55So an example of one thing that we could do with a my h2 is I could go to one of the built-in
08:00Properties which is called inner
08:02HTML and I could go ahead and say change this copy to
08:07NARF or how about we change this copy to my name or to our brand name, which is zero to geek and
08:13If I save this and I go back into my browser and click on refresh
08:17We'll see that my headline copy is gonna update now
08:21The the power here is obviously immense
08:24but one of the disadvantages of using directly with the built-in functions in this case is that
08:31If you're working with an older browser and older
08:34This is an HTML 5 JavaScript new function
08:37So if if you're working on an older browser
08:40Well, this wouldn't work and then you would have problems with compatibility
08:44and that's exactly where jQuery comes in and becomes incredibly useful because it cuts down all those a lot of those issues of
08:52compatibility now before we go and look into how to work with jQuery again and see how would we do this if we were
08:58Doing this in jQuery. I want to talk about another built-in
09:02Object that exists and even before that we could see that we have your a document
09:07It's an object that exists already
09:09We can't really have two documents because every application has every web page has one document and you can see how that document has
09:18Functions and there's many functions that help us interact with our document and one one of them was that get element by ID
09:43Now there's many objects that don't exist just on their own
09:47Automatically and those type of objects we want to create them
09:51Now the reason is is sometimes contrary to things that already are on the stage
09:57There's many scenarios where we'd want to create a completely new element
10:02Because we need to use it for a specific thing
10:04But if we didn't need to use it then there's no reason for it to be created already now
10:10In the next lesson what we're gonna do is we're gonna look at the first
10:14pre
10:15First way to first time we ever look at it how to create an object on our own although we've created objects before
10:20We've done it without knowing we're creating objects in the next lecture. That's exactly what we're gonna do
10:26We're gonna create our first
10:28Object together and that object is gonna be of a type of date and we're gonna see how the date object works and and how
10:35What what it does by default or at least a little bit of it
10:38So we'll see you in the next lesson where we dive a little bit deeper into the world of objects

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!