The JavaScript Survival Guide

Fireship
15 Jul 201914:47

Summary

TLDRهذا النص هو ملخص لفيديو تعليمي يغطي أساسيات لغة برمجة جافا سكريبت، تتضمن المقدمة المزحة التي تقارن جافا سكريبت بفيروس ويناقش كيفية تشغيل الكود في المتصفح والخادم. يغطي النص المفاهيم الأساسية مثل الأنواع الأساسية للبيانات، التحكم في التدفق، المتغيرات، ووظائف جافا سكريبت. يتضمن أيضًا مناقشة المفاهيم المتقدمة مثل الإغلاق والرفع. النص يهدف إلى تعزيز فهم البرمجة الجافا سكريبت والمهارات على التعامل مع الجوانب الغير تقليدية للغة.

Takeaways

  • 😀 جافاسكريبت يُعرف بـ 'فيروس'، مما يعني أن مطورو البرمجيات هم 'المضيف' للغة برمجة جافاسكريبت.
  • 🔍 في المستقبل قد تصبح جزءًا من مطورو جافاسكريبت، لذا يجب أن تكون مستعدًا للتعامل مع المفاهيم الأساسية للغة برمجة جافاسكريبت.
  • 📝 يمكن تشغيل جافاسكريبت على الخادم والعميل، ويمكنك إنشاء تطبيق جافاسكريبت بسيط مثل 'مرحبا بالعالم'.
  • 🌐 يمكن تشغيل جافاسكريبت في المتصفح عن طريق إنشاء علامة script في HTML واستخدام خاصية defer لتحميل السكريبت بعد تحميل الوثيقة.
  • 📚 في جافاسكريبت، هناك سبع أنواع أساسية من القيم الأصلية، بما في ذلك السلسلة، العدد، المنطقية، الفارغة، الغير محدد، وغير موجود.
  • 🔒 القيم الأصلية في جافاسكريبت لا يمكن تغييرها بشكل مباشر، وهي تعتبر قيمة ثابتة.
  • 🔄 جافاسكريبت هي لغة ضعيفة النوع ديناميكيًا، مما يعني أنه يمكن استخدام نوع القيمة في وقت التشغيل إذا لزم الأمر.
  • 🔄 الكائنات في جافاسكريبت تمثل هياكل بيانات معقدة مثل القوائم والكائنات والوظائف، ويمكن تغييرها بعد تخصيصها.
  • 🔄 في جافاسكريبت، يمكن أن تكون القيم المنطقية 'صحيح' أو 'خطأ'، وهذا يعتمد على نوع القيمة ومحتوىها.
  • 🔄 يمكن أن تكون الوظائف في جافاسكريبت مجهولة أو معرفة، ويمكن أن تكون مدخلات أو مخرجات لوظائف أخرى، مما يُعرف بـ 'وظائف ترتيب أعلى'.
  • 🔒 يمكن أن تكون الوظائف في جافاسكريبت مغلقة، مما يعني أن الدالة الداخلية يمكنها الإشارة إلى متغيرات خارج الدالة المحيطة بها حتى بعد الانتهاء منها.
  • 🏷️ في جافاسكريبت، يمكن أن تكون الكائنات مكونًا من أزواج من المفتاح والقيمة، ويمكن تغيير خصائص الكائن بعد إنشائها.

Q & A

  • ما هي البرمجة الأساسية لغة جافا سكريبت؟

    -البرمجة الأساسية للغة جافا سكريبت تشمل المفاهيم الأساسية مثل العمليات الأساسية للغة ووظائفها وكيفية تشغيل الكود.

  • كيف يمكنني تشغيل كود جافا سكريبت؟

    -يمكنك تشغيل كود جافا سكريبت مباشرة على الماكينة عبر الخط السفلى باستخدام node.js أو في متصفح الويب من خلال علامة script في HTML.

  • ما هي الفرق بين الأنواع الأساسية والكائنات في جافا سكريبت؟

    -الأنواع الأساسية هي قيم غير قابلة للتغيير مثل السلسلة والعدد والمنطقية، بينما الكائنات مثل القوائم والعمليات يمكن أن تتغير بعد التعيين.

  • لماذا يُعتبر جافا سكريبت 'فيروسًا'؟

    -هذا التعبير مجرد مقارنة تشير إلى انتشار واسع جافا سكريبت وتأثيرها الineluctable على البرمجة moderna.

  • ما هي الimmelance في جافا سكريبت؟

    -الhoisting هي عملية جافا سكريبت التي ت置 التعريفات الأساسية للمتغيرة والوظائف في الأعلى من السياق التنفيذي.

  • كيف يمكنني إنشاء تطبيق 'hello world' بلغة جافا سكريبت؟

    -يمكنك إنشاء ملف باسم 'index.js' و在里面写下 'console.log("hello world")'، ثم تشغيله عبر الخط السفلى بكتابة 'node index.js'.

  • ما هي الفرق بين 'var' و 'let' و 'const' في جافا سكريبت؟

    -'var' هي الطريقة القديمة للتعريف المتغيرة، 'let' هي للنطاقات المحدودة، و 'const' هي للقيم التي لا يمكن تغييرها.

  • ما هي الوظائف في جافا سكريبت؟

    -الوظائف في جافا سكريبت هي قطعة من الكود التي تأخذ مدخلات وتولد ناتج عندما تتم استدعائها.

  • ما هو ال뜻 بـ 'this' في جافا سكريبت؟

    -'this' هو مفتاح鲨 يشير إلى الكائن الحالي الذي يشغل الكود، قد يختلف هذا الكائن بناءً على سياق الوظيفة.

  • ما هي ال分别是 'Truthy' و 'Falsy' في جافا سكريبت؟

    -'Truthy' هي القيم التي ت coiercible إلى 'true' في الشروط، مثل الأرقام والكائنات، بينما 'Falsy' هي القيم التي ت coiercible إلى 'false' مثل السلسلة الفارغة والعدد 0.

  • ما هي ال用处 لـ '===' في جافا سكريبت؟

    -'===' هو العامل المساواة الصارف الذي يتحقق من المساواة في النوع والقيمة معًا، مما يوفر سلوكا أكثر توقعاً في المنطق الشرطي.

Outlines

00:00

😀 JavaScript: Survival Guide and Basics

This paragraph introduces the necessity for developers to understand JavaScript, comparing it to a virus in terms of its ubiquity. The video aims to prepare viewers for the practical aspects of JavaScript programming, including handling its peculiarities. It starts with the fundamental concept of running JavaScript, such as creating a 'Hello World' application using an editor, command line, and within a browser environment. The importance of understanding script tags and their attributes like 'defer' is highlighted, especially for those not using frameworks that abstract this process away.

05:00

📚 JavaScript Fundamentals: Primitives, Objects, and Control Flow

The second paragraph delves into the core building blocks of JavaScript: primitives and objects. It explains the seven primitive types, their immutability, and how JavaScript handles type checking at runtime. The paragraph also contrasts these with objects, which are mutable and can represent complex data structures. The concept of 'truthy' and 'falsy' values in conditional statements is explored, along with logical operators and the difference between abstract and strict equality checks. The summary also touches on variable hoisting and the importance of understanding execution context in JavaScript.

10:01

🔧 Control Flow and Variable Declaration in JavaScript

This section discusses control flow mechanisms in JavaScript, such as if statements, else-if clauses, and the ternary operator. It emphasizes the importance of understanding truthy and falsey values, as well as the use of logical operators like '&&' and '||'. The paragraph also introduces the switch statement and try-catch blocks for error handling. A deeper dive into variable declaration is provided, explaining the differences between 'var', 'let', and 'const', and the concept of hoisting. The execution context and the implications of variable scope are also covered, along with best practices for variable declaration.

🎯 Functions and Closures in JavaScript

The focus of this paragraph is on functions in JavaScript, starting with their definition and usage. It explains the difference between named and anonymous functions, as well as higher-order functions that can take other functions as arguments or return them as values. The concept of closures is introduced, describing how an inner function can access variables from an outer function even after the outer function has completed execution. The paragraph also touches on the use of 'this' keyword within functions and how it can lead to different contexts depending on the function type (regular vs. arrow functions).

🛠 Understanding Objects and 'this' in JavaScript

The final paragraph discusses objects in JavaScript as key-value pair collections and how they can be manipulated using dot notation or bracket notation. It explains how functions can be properties of objects and how 'this' can be used to reference the object within its methods. The paragraph clarifies the difference in 'this' binding between regular functions and arrow functions, and how to use the 'call' method to explicitly set the 'this' context. It also addresses the challenges of understanding 'this' in various scenarios, such as within the global scope or custom objects.

Mindmap

Keywords

💡جافاسكريبت (JavaScript)

جافاسكريبت هو لغة برمجة côté العميل (Client-Side) تستخدم عادة في تطوير الويب لجعل الصفحات الإلكترونية تفاعلية. في النص، يُستخدم المصطلح 'جافاسكريبت' لوصف اللغة كجزء أساسي من تطوير الويب، ويُذكر أنه يمكن أن يتسبب في بعض المشاكل التي يمكن أن تواجه مطوري الويب في مسارهم.

💡مبرمج (Developer)

المبرمج هو الشخص الذي يستخدم جافاسكريبت لتطوير البرمجيات. في النص، يُستخدم المصطلح 'المبرمج' لوصف الشخص الذي يمكن أن يواجه تحديات ومفاهيم جديدة في عالم جافاسكريبت.

💡البرمجة العملية (Practical Programming)

البرمجة العملية هي القدرة على استخدام المفاهيم الأساسية لبناء البرمجيات العملاقة. في النص، يُشدد على أهمية فهم المفاهيم الأساسية في جافاسكريبت للبرمجة العملية.

💡النواة (Under the Hood)

النواة هنا تشير إلى فهم التفاصيل التقنية الأساسية لجافاسكريبت. في النص، يُذكر كيف يعمل جافاسكريبت في الخلفية، مما يساعد المبرمج على فهم العمليات التي تحدث عندما يكتب الكود.

💡ملف الelcomer (Hello World App)

ملف الelcomer هو مثال بسيط يستخدم عادة لتوضيح كيف يعمل لغة برمجة معينة. في النص، يُستخدم مثال 'console.log(hello world)' لتوضيح كيف يمكن للمبرمجين بدء استخدام جافاسكريبت.

💡النص البرمجي الخادمي (Back-End Server Side JavaScript)

النص البرمجي الخادمي هو الكود الذي يشغل على الخادم وليس على العميل. في النص، يُشرح كيف يمكن للمبرمجين استخدام جافاسكريبت لكتابة البرمجيات الخادمية.

💡المتصفح (Browser)

المتصفح هو الأداة التي يستخدمها المستخدم لتصفح الويب. في النص، يُستخدم المصطلح 'المتصفح' لتوضيح كيف يمكن لجافاسكريبت أن تشغل ضمن المتصفح وكيف يمكن للمبرمجين استخدام النص البرمجي في الصفحات الإلكترونية.

💡البرمجة الأساسية (Fundamental Features)

البرمجة الأساسية هي الميزات التي تعتبر أساسًا في لغة برمجة. في النص، يُناقش العديد من الميزات الأساسية في جافاسكريبت التي تعتبر مهمة لفهم البرمجة العملية.

💡البرمجة المنطقية (Control Flow)

البرمجة المنطقية هي الطريقة التي يتم بها اتخاذ القرارات في الكود. في النص، يُناقش مفهوم الTruthy والFalsy في جافاسكريبت وكيف يؤثر ذلك على البرمجة المنطقية.

💡المتغيرات (Variables)

المتغيرات هي الوحدات الأساسية التي تحفظ البيانات في البرمجة. في النص، يُناقش الفرق بين أنواع المتغيرات في جافاسكريبت مثل 'var'، 'let'، و 'const' وكيف يؤثر ذلك على البرمجة.

💡الوظائف (Functions)

الوظائف هي الوحدات الأساسية في البرمجة التي تؤدي وظائف معينة. في النص، يُناقش مفهوم الوظائف في جافاسكريبت وكيف يمكن استخدامها في البرمجة.

💡الكائنات (Objects)

الكائنات هي هيكل بيانات يحتوي على مجموعة من الخصائص. في النص، يُشرح كيف يمكن استخدام الكائنات في جافاسكريبت لتنظيم البيانات.

💡الإغلاق (Closures)

الإغلاق هو مفهوم في جافاسكريبت يتيح لوظيفة الوصول إلى متغيرات خارج النطاق الذي تم إنشاؤها. في النص، يُشرح الإغلاق وكيف يعمل في جافاسكريبت.

💡Truthy والFalsy (Truthy and Falsy)

Truthy والFalsy هي القيم التي تحدد ما إذا كانت قيمة صحيحة أو خاطئة في سياق الشروط. في النص، يُناقش القيم التي تعتبر truthy أو falsy في جافاسكريبت.

Highlights

JavaScript is sometimes referred to as a 'virus', implying developers are the 'host'.

The necessity for developers to understand JavaScript's fundamental concepts for survival in the industry.

Introduction to a full JavaScript course focusing on practical programming and interview preparation.

How to run JavaScript code by creating a 'Hello World' application using a simple script.

Explanation of executing JavaScript via the command line with Node.js.

The role of script tags in HTML for browser-based JavaScript execution.

The importance of understanding how script tags work in web development.

JavaScript's seven primitive types: string, number, boolean, null, undefined, symbol, and bigint.

Immutability of primitive types and their inability to be changed directly after assignment.

JavaScript's dynamic and weakly typed nature, allowing for runtime type checks with the typeof operator.

Differences between 'undefined' and 'null', and their respective use cases.

Objects as complex data structures in contrast to primitive types, allowing for mutation.

Concept of 'hoisting' in JavaScript and its implications for variable and function declarations.

The introduction of 'let' and 'const' as alternatives to 'var' for better scope management.

JavaScript's control flow mechanisms, including if statements, else-if, and logical operators.

The concept of 'truthy' and 'falsy' values in JavaScript and their impact on conditional statements.

Explanation of equality operators in JavaScript: abstract comparison with '==' and strict equality with '==='.

Use of the ternary operator for concise variable assignment based on conditions.

JavaScript's 'switch' statement as an alternative to multiple if-else conditions.

The use of 'try-catch' blocks for error handling in JavaScript.

Differences between 'var', 'let', and 'const' in terms of variable scope and reassignment.

Fundamentals of JavaScript functions, including definition, inputs, outputs, and higher-order functions.

The concept of closures in JavaScript and their ability to maintain access to outer function's state.

Introduction to objects in JavaScript as key-value pair collections and their mutability.

The 'this' keyword in JavaScript and its reference to the current object in the code's context.

Different ways to call functions in JavaScript, including using the 'call' method to specify 'this' context.

Transcripts

play00:00

some people call javascript a virus and

play00:02

if that's true it means developers are

play00:04

the host no matter what you label

play00:05

yourself ads today you have to face the

play00:07

reality that one day you may join the

play00:09

hordes of JavaScript developers out

play00:10

there and when that day comes you need

play00:12

to be prepared by understanding the

play00:14

practical fundamental concepts of the

play00:15

JavaScript programming language today's

play00:17

video is not about victory it's about

play00:19

survival welcome to episode 3 of the

play00:26

full JavaScript course in the last video

play00:28

we looked at how JavaScript works under

play00:29

the hood and today we'll look at many of

play00:31

the fundamental features of the language

play00:32

as they relate to practical programming

play00:34

but more importantly I want to help you

play00:36

survive some of the weirder parts of

play00:37

JavaScript that might trip you up or

play00:39

that you might find on an interview

play00:40

question and many of these concepts like

play00:42

closures and hoisting can be easily

play00:44

understood by removing the head or

play00:46

destroying the brain

play00:50

chapter 1 where and how do I run my

play00:53

JavaScript code let's find out by

play00:54

creating a hello world app open up an

play00:56

editor like vs code create a file called

play00:58

index J s and type in console.log hello

play01:02

world now assuming you have no js'

play01:04

installed you can run the script

play01:05

directly on your machine via the command

play01:07

line we'll have an entire video

play01:08

dedicated to node.js later in the course

play01:10

but all you need to know for right now

play01:12

is that if you type in node index J s in

play01:14

the command line it will execute the

play01:16

code in your script so you just wrote a

play01:18

back-end server side JavaScript

play01:19

application but when most people think

play01:21

about J s they're thinking about

play01:22

browsers and web applications it's

play01:25

possible to run code in the browser just

play01:26

like we did from the command line by

play01:28

opening the developer console in the

play01:30

previous video we learned that

play01:31

javascript has an interpreter or JIT

play01:33

compiler to be precise that will execute

play01:35

your code when you type it in here but

play01:36

the browser console is really just a

play01:38

debugging tool the way that web

play01:40

applications actually work is they

play01:41

declare a script tag in an HTML document

play01:43

we can go ahead and create an index.html

play01:45

file then we'll add a script tag and set

play01:48

the source attribute to the JavaScript

play01:50

file when the browser parses this HTML

play01:53

file it's going to see the script tag

play01:54

and then try to load and execute the

play01:56

script in our case it won't execute the

play01:58

JavaScript until the document is fully

play02:00

loaded because we use this defer

play02:01

attribute this is useful because a lot

play02:03

of times your JavaScript will reference

play02:04

HTML elements in the body which won't be

play02:07

available until the document is fully

play02:09

loaded now the big caveat here is that

play02:11

most developers are using frameworks

play02:12

which will set up the script tag for you

play02:14

but if you're doing any kind of web

play02:15

development it's important to understand

play02:17

how script tags work but now it's time

play02:19

to move on to chapter 2 primitives and

play02:21

objects which are two of the lowest

play02:23

level building blocks in the language

play02:24

there is a total of seven primitive

play02:26

types in modern JavaScript with the most

play02:28

common ones being string number boolean

play02:31

null and undefined a special

play02:32

characteristic of primitives is that

play02:34

they're immutable which means their

play02:35

value can't be directly changed once

play02:37

they've been assigned to a variable they

play02:39

can be reassigned to a different value

play02:40

but they just can't be changed directly

play02:42

and because JavaScript is a dynamic

play02:44

weakly typed language we don't actually

play02:46

use type annotations in our code but we

play02:48

can use this type of operator to check a

play02:50

type at runtime if needed and just for

play02:52

those who are wondering I'm using the

play02:53

quota plugin for vs code which displays

play02:56

the output of my code on the right here

play02:57

in blue look at these data types in more

play02:59

detail as we go through the video but

play03:01

for right now you should know the

play03:02

difference between undefined in

play03:04

Knowle undefined is the default value

play03:06

that you'll see for a variable that

play03:07

doesn't have a value assigned to it or

play03:09

for a function that just returns nothing

play03:10

Knowle is similar in the sense that it

play03:12

represents an empty value but it's

play03:14

something that you the developer would

play03:15

assign explicitly now these primitive

play03:18

data types can be contrasted with the

play03:19

object type which represents more

play03:21

complex data structures like arrays

play03:23

objects and functions the big difference

play03:25

is that objects can be seen as a

play03:27

collection of keys and values or

play03:28

properties and they can be mutated after

play03:30

being assigned to a variable so the

play03:32

bottom line that you need to understand

play03:33

is that anything that's not a primitive

play03:35

type is going to inherit from object

play03:37

that includes things like functions

play03:38

arrays or any class instances now

play03:41

there's one other little piece of

play03:42

weirdness that you might encounter and

play03:44

that is primitive wrapper objects the

play03:46

reason I bring this up is because you

play03:47

may see these out there in the wild and

play03:49

they allow you to basically wrap a

play03:50

primitive type in a class instance but

play03:53

you should never really use them unless

play03:54

you have a good reason to chapter 3

play03:56

control flow and truthy versus falsey in

play03:59

JavaScript we can implement conditional

play04:01

logic with an if statement if a

play04:03

condition is true the-- then we execute

play04:04

this block of code else we execute this

play04:06

other block of code and we can also use

play04:08

else--if if there are multiple

play04:10

conditions to check but the more

play04:12

important thing to understand here is

play04:13

what is truthy and what is false e in

play04:15

JavaScript because JavaScript will

play04:17

always try to coerce a value into a

play04:19

boolean when it's encountered inside of

play04:20

a conditional statement true is

play04:22

obviously true the-- and anything that's

play04:23

an object is also true the-- even if

play04:25

it's an empty array or an empty object

play04:27

the ones you have to watch out for are

play04:29

strings and numbers a string that has

play04:31

length will be true the-- but an empty

play04:33

string will be false e then the number 0

play04:35

will be false e but all other numbers

play04:37

will be true the-- and if you're ever

play04:39

unsure about a value you can simply add

play04:40

a double bang in front of it to coerce a

play04:42

to a boolean yourself now the reason

play04:44

this works is because the exclamation

play04:46

mark is a logical not operator a single

play04:49

exclamation mark will return false if a

play04:51

value can be converted to true so if you

play04:53

add two of them then it will give you

play04:54

the actual boolean that that value will

play04:56

be course two and what we're talking

play04:58

about logical operators you should also

play05:00

know that a double ampersand is logical

play05:02

and which ensures that all expressions

play05:04

in the condition will be converted to

play05:05

true and we also have logical or which

play05:08

returns true if at least one of the

play05:09

expressions is true the-- now another

play05:11

thing you should know is that JavaScript

play05:12

provides two different equality

play05:14

operators a double equal sign is called

play05:16

an abstract

play05:17

comparison and it will try to make a

play05:18

type conversion before actually running

play05:20

the comparison that's pretty weird

play05:22

so most linters will tell you to never

play05:23

use it in contrast the strict equality

play05:30

operator will check for equality on both

play05:32

the type and the value which will give

play05:34

you much more predictable behavior when

play05:36

it comes to conditional logic it can get

play05:38

pretty verbose if you use an if

play05:39

statement to define a variable you'll

play05:42

commonly see this done with the ternary

play05:43

operator which gives you a shorthand

play05:45

syntax for defining a variable based on

play05:47

an if-else statement it has a logical

play05:49

condition on the left side then a

play05:51

question mark then the first value is

play05:53

what will be assigned if the condition

play05:54

is true the-- and the second value after

play05:56

the colon is the value if it's false II

play05:58

now before we leave the topic of control

play06:01

flow it's also worth noting that

play06:02

javascript has a switch statement which

play06:04

allows you to start with an expression

play06:06

and then compare it to multiple cases

play06:08

and this can be a good alternative to

play06:09

if-else statements if you have a lot of

play06:11

conditions to check and we can also wrap

play06:13

our code in a try-catch statement which

play06:15

will attempt to run the code in the try

play06:17

block but if an error is thrown then it

play06:19

will move to the catch block you can try

play06:21

this out yourself by using throw inside

play06:23

of the try block and then you'll see

play06:24

that it runs the code inside of catch

play06:26

and provides that error that was thrown

play06:27

up above and try if an error is not

play06:30

thrown then the code inside of catch

play06:31

will never be run and you can also add a

play06:33

finally block to the end of all this to

play06:35

run some code after both try and catch

play06:37

have been executed and finally a full

play06:39

six and a half minutes into this video

play06:40

we're ready for chapter four variables

play06:42

we'll look at the difference between

play06:43

varlet and const and also figure out

play06:46

what hoisting means borrow allows you to

play06:48

initialize a variable assign a value to

play06:50

it and then also reassign a value to it

play06:52

later now before you can really

play06:53

understand anything else about variables

play06:55

you need to understand the execution

play06:57

context and this simply refers to the

play06:59

way your code is being interpreted

play07:00

because your JavaScript program likely

play07:02

contains variables and functions defined

play07:04

all over the place

play07:05

so the context defines the relationship

play07:07

between how your code is written to how

play07:08

it will be interpreted by the JavaScript

play07:10

engine we can define a variable anywhere

play07:12

in our script and that will be

play07:14

considered a global variable in other

play07:15

words it's available in the global

play07:17

execution context that means if I have a

play07:19

function somewhere else in my code I can

play07:21

still reference that variable I like to

play07:23

think of global as an imaginary function

play07:25

that wraps my script in other words

play07:27

running a script is just like calling

play07:29

this imaginary glow

play07:30

function and that means if we define

play07:31

additional functions in our code we have

play07:33

a different execution context for

play07:35

defining variables for example if we try

play07:38

to define a variable inside this

play07:39

function and then reference it in the

play07:41

global context we'll get a reference

play07:43

error because the local variable is not

play07:45

available in this context now that's

play07:47

pretty straightforward

play07:48

but there are a few pieces of weirdness

play07:49

that you need to be aware of if you

play07:51

assign a value to an undeclared variable

play07:53

which you should really never do it will

play07:55

automatically be assigned as a global

play07:56

variable even if it's done inside of an

play07:58

enclosing function and the other thing

play08:00

you need to know about is hoisting which

play08:02

applies to variables but also function

play08:04

declarations as well now whenever

play08:05

JavaScript processes in execution

play08:07

context it will basically put all the

play08:09

variables at the top or in other words

play08:11

hoist them to the top of the context

play08:13

that means if we initialize a variable

play08:14

down here at the bottom and then

play08:16

reference it somewhere higher up in the

play08:17

code it will still be considered

play08:18

declared within this scope but the

play08:20

actual assignment of a value will still

play08:22

happen wherever you define it in the

play08:24

code so if you want to keep your sanity

play08:26

it's always a good idea to declare your

play08:27

variables at the top of the context but

play08:29

the bigger problem with var is that it

play08:31

becomes very hard to keep track of the

play08:32

scope of variables and there can be a

play08:34

lot of name collisions as your app grows

play08:36

more complex luckily we no longer have

play08:38

to use var because in modern JavaScript

play08:40

we have let and Const let is similar to

play08:42

var in most ways but it's limited to the

play08:44

scope of a block statement that means if

play08:47

you define a variable inside of an if

play08:48

statement or a loop it will be limited

play08:50

to that block and this differs from bar

play08:52

which would leak out into the parent

play08:54

scope and also like var variables

play08:56

defined with let can be a reassigned to

play08:58

different values later in the code but

play08:59

in many cases you'll have values that

play09:01

should never be reassigned this makes it

play09:03

a lot more difficult to accidentally

play09:05

override values in your code so when it

play09:06

comes to variables a good rule to follow

play09:08

is to always use Const unless you

play09:10

absolutely know that you'll override

play09:12

that value later in which case you can

play09:13

use let and you should respect the

play09:15

legacy of our but just never use it and

play09:17

with that we're ready for chapter 5

play09:19

functions

play09:21

I think you don't say hello this course

play09:24

will have an entire video on functions

play09:26

but there are a few basic things you

play09:27

should know so let's start by answering

play09:29

the question of what is a function and

play09:31

the answer is that it's simply a piece

play09:33

of code that takes an input and produces

play09:35

an output when it's called you can use

play09:37

the function keyword as we're doing here

play09:38

or you can use the more concise arrow

play09:40

syntax in modern JavaScript if you omit

play09:42

braces the code following the arrow will

play09:44

implicitly return a value so you can

play09:46

define a function that returns something

play09:48

on a single line

play09:49

but as easy as that sounds there's all

play09:51

kinds of complexity and terminology that

play09:53

goes along with functions so let's try

play09:55

to unpack some of the most important

play09:56

concepts that you should understand as

play09:58

ajs developer first of all functions can

play10:00

be anonymous or they can have a name

play10:02

which will immediately follow the

play10:03

function keyword and you'll also

play10:05

commonly see anonymous functions

play10:06

assigned to the value of a variable and

play10:08

another thing you'll hear is that

play10:09

JavaScript supports higher-order

play10:11

functions and that means we can provide

play10:12

functions as the input or arguments to

play10:15

another function or as the return value

play10:17

from a function in the previous video we

play10:19

talked about the JavaScript event loop

play10:20

and callbacks and this is important with

play10:22

functions because you'll often use

play10:24

anonymous functions as arguments to

play10:26

other functions that will be called back

play10:27

later after some asynchronous code is

play10:29

finished executing in addition you can

play10:32

define new functions within a function

play10:33

and this example the outer function is

play10:36

the one that wraps the inner function

play10:37

and that's related to a very important

play10:39

question that you'll hear in a lot of

play10:40

JavaScript interviews what is a closure

play10:42

in the most simple sense a closure is

play10:44

just a function within a function where

play10:46

the inner function references a variable

play10:48

that was declared in the scope of the

play10:50

outer function now what makes this

play10:52

special versus a lot of other

play10:53

programming languages is that the

play10:55

variable in the outer function will be

play10:57

maintained in memory even after that

play10:59

function returns and has popped off the

play11:00

call stack so this means the inner

play11:02

function always has access to this state

play11:04

from the outer function at the time it

play11:06

was created in the code here you can see

play11:07

we define a couple of variables in the

play11:09

outer function then we operate on them

play11:11

and return them from the inner function

play11:13

and then expose the inner function by

play11:15

simply returning it and now because that

play11:17

outer function has closed over the inner

play11:19

function we can call it and still have

play11:21

reference to the state in the outer

play11:23

function even though we only called the

play11:24

outer function once we still have access

play11:26

to its local variables and that's how

play11:28

you make a closure if you have

play11:30

experience with an object-oriented

play11:31

programming language a closure is very

play11:33

similar to a class

play11:34

instance at least from a conceptual

play11:36

standpoint because you have a function

play11:38

that contains some state and then you

play11:39

have an inner function that can operate

play11:41

and change that state in the same way

play11:43

that a class instance has some

play11:44

properties and then you have methods

play11:46

that can change those properties in fact

play11:48

the class keyword in JavaScript is just

play11:50

syntactic sugar for functions and

play11:52

closures but we'll look at classes and

play11:54

object-oriented programming in a future

play11:56

video because now we're ready for

play11:57

chapter 6 objects an object is just a

play12:00

data structure that allows you to

play12:02

associate a collection of key value

play12:04

pairs which is similar to a map or a

play12:06

hash map in other languages it's

play12:08

possible to instantiate an object and

play12:09

then add properties to it one by one but

play12:11

in most cases you'll use the object

play12:13

literal syntax this allows you to define

play12:15

an object by simply enclosing your key

play12:17

value pairs inside of braces you can

play12:19

access the value of a key by using dot

play12:21

notation or with brackets and a string

play12:24

and you can mutate properties on an

play12:26

object even when it's defined as a Const

play12:28

variable and it's also important to

play12:30

point out that functions can be used as

play12:31

values on an object then inside a

play12:34

function we can actually reference

play12:35

properties on this object by using this

play12:37

but what the hell is this this is one of

play12:41

the more challenging concepts to grasp

play12:42

in JavaScript now remember earlier I

play12:45

said that anything that's not a

play12:46

primitive value is an object and this is

play12:48

just a keyword that refers to the

play12:50

current object that the code is

play12:51

operating in if you go into the browser

play12:54

console and say console like this it's

play12:56

going to print out the window object

play12:57

that's because window is the global

play12:59

scope and browsers when referenced by

play13:01

itself or if used in a function that's

play13:03

called normally it will reference the

play13:05

global object that's easy enough but

play13:07

what if we have our own custom object

play13:09

and on that object we have two

play13:10

properties that define functions the

play13:12

first function uses the function keyword

play13:14

but the second function it uses the

play13:16

arrow syntax now intuitively you might

play13:18

think that these functions are exactly

play13:20

the same but that's not the case in the

play13:22

first function this refers to the object

play13:24

that it's defined on but when it comes

play13:26

to the arrow function it doesn't have

play13:27

its own bindings to this which means it

play13:29

bypasses our custom function and this

play13:31

becomes the global object now the other

play13:34

thing you should know is that when you

play13:35

have a function there are other ways to

play13:37

call it beyond just using parentheses

play13:39

for example you can use it's call method

play13:41

and in pass in the this context that you

play13:43

want to buy into the function now any

play13:45

references that you have to this and the

play13:47

function will

play13:48

prints the object that you passed a call

play13:50

in the code here we're calling this face

play13:52

and the function but it's currently

play13:53

bound to the global object which doesn't

play13:56

have a face property but if we pass in

play13:57

an object that does have a face property

play13:59

like a clown or a ghost then it will

play14:01

return a defined value understanding

play14:04

this will make you a better JavaScript

play14:05

developer but it won't be something that

play14:07

you have to use on a daily basis and

play14:09

with that being said I think we've

play14:10

covered enough information for one video

play14:12

and don't worry too much if that felt

play14:13

overwhelming we can't have anyone

play14:15

freaked out out there ok gotta keep our

play14:17

composure we'll revisit many of these

play14:25

concepts when we look at the practical

play14:26

applications of functions objects and

play14:29

more I'm gonna go ahead and wrap things

play14:30

up there if this video helped you please

play14:32

like and subscribe and make sure to

play14:33

check out the full source code on fire

play14:35

ship IO

play14:36

thanks for watching and I will talk to

play14:38

you soon

play14:39

[Music]

play14:45

you

Rate This

5.0 / 5 (0 votes)

相关标签
برمجةجافا سكريبتدورةمفاهيمأخطاءبرمجة ويبتطويربرمجة متقدمةبرمجة أساسيةتحليلاتبرمجة تطبيقات
您是否需要英文摘要?