You are on page 1of 8

Using " use strict":

This is why we use strict mode:


• Enhanced Error Handling:
In strict mode, certain actions that would otherwise result in silent errors or warnings
are treated as explicit errors. For example, assigning a value to an undeclared variable
or using a non-writable global variable will throw an error, making it easier to catch
and fix such mistakes.

• Elimination of Implicit Global Variables:


In non-strict mode, omitting the var, let, or const keyword when declaring a
variable creates an implicit global variable. Implicit global variables can lead to
unexpected behavior and make code harder to maintain. Strict mode requires explicit
variable declaration, preventing accidental creation of global variables.

• Restriction of Octal Numeric Literals:


In strict mode, octal numeric literals (e.g., 0123) are not allowed. In non-strict mode,
these literals are treated as decimal numbers if they don't conform to valid octal
syntax. Strict mode helps avoid confusion and promotes clearer code.

• Prohibition of Duplicate Parameter Names:


Strict mode does not allow duplicate parameter names in function declarations or
function expressions. In non-strict mode, duplicate parameter names are silently
ignored, which can lead to unintended consequences and bugs.

• Secure "this" Binding:


In strict mode, the this value inside a function is not automatically coerced to the
global object (window in a web browser). Instead, it remains undefined in functions
that are not methods or constructors, helping to avoid potential security
vulnerabilities.

Introduction to Functions
Function as an expression itself

object ‫ وال‬،object ‫فه بالنهاية‬


‫ ي‬،expression ‫ عبارة عن‬functions‫تعتب ال‬
‫ر‬
‫ قد تكون‬functions‫ القيمة المرجعة من ال‬،‫ بكل الحوال‬،expression ‫هو‬
‫ وقد ال تكون‬expression

‫تعتب‬
‫ تعيد قيمة ==< القيمة المرجعة ر‬function ‫ يف حال كانت ال‬-
‫ معقدة أ كب‬expression ‫ ويمكن دمجها ضمن‬expression
:‫ مثال‬-
- Function x(){return `test`}
- Console.log(`this is ${x()}`) ➔ "this is test"
- Console.log (typeof (x())); ➔ "string"
- Console.log (typeof(x)); ➔ function (built-in object)

‫ه‬
‫ نفسها ي‬function ‫ فإن ال‬،‫ ال تعيد قيمة‬functions ‫ يف حال ال‬-
‫ ليس‬function ‫ لكن القيمة النهائية لل‬،expression (object)
‫وف حال جربنا سبى أنه ال يمكن أن‬
‫ ي‬،statement ‫ بل‬، expression
‫ أكب تعقيدا‬expressions ‫نضعها ضمن‬

:‫ مثال‬-
- Function x(){console.log(`test`)}
- Console.log(`this is ${x()}`) ➔ "this is undefined"
- Console.log(typeof(x())) ➔ undefined
- Console.log(typeof (x)) ➔ function (built-in object)
-

Anonymous Function

The classical function is named function like this:

- Function function_name (){}

However this is an Anonymous function:

- Function (){}

Function declaration and function expression

There is 2 ways to make a function:

1- function declaration defining

- Function x(){};

2- function expression defining (‫ال عالقة لها بال‬expression ‫)السابقة‬


‫يعن أن‬ ‫ى‬
‫ ال ي‬،expression ‫ بطريقة ال‬function ‫ بمعن أنه حن لو عرفنا‬-
‫ى‬
‫القيمة المرجعة ي‬
‫ بل يمكن‬expression ‫الن بداخلها يجب أن تكون حرصا‬
)"statement"( ‫ ال تعيد قيمة‬function ‫أن تكون‬

- Const x = function(){}; //Anonymous function

◼ Only With function expression, we can define an arrow


function, and of course the arrow function itself is a value
with typeof Function and can be used as the same as any
other value, we just cant declare it using function
declaration that's al

Arrow function

It was added to ES6, it is a shorthand of expression


function defining, and can't standalone to define a
function like this:

- (arg1)=> {//code} ➔ this is wrong

But it has to be a part of expression function like this:

• Const x= arg1 => arg1 + 1; ➔ this is right


• Const x= arg1 => {sum = arg1 + 1; return sum} ➔ this is
right
• Const x= (arg1,arg2) => arg1 + arg2; ➔ this is right
• Const x= (arg1,arg2) => { sum= arg1 + arg2; return sum}
➔ this is right
Going deeper with primitive and objects,
with expressions and statements
The code is rather "expression" or "statement"

The expression ➔ value, and the value ➔ primitive or object

==== we spoke about primitive values, and they was: Number,


String, Boolean, undefined, Symbol

=== now we speak about objects values

- object values are all first class objects, which means that we
can assign the objects to variable, sending it as argument, and
return it from functions.

We have now two kinds of objects

1- user-defined objects (custom objects), and they are like this:

The typeof() for it is ➔ object

+ Literal objects: // object ‫طاير بالهوا‬

Const p1 = {

"name": "karim",

"sayhello": function(){

Console.log(`hello world`);

}
Console.log(typeof(p1))➔ object

+ Instance objects: ‫عب دالة بانية‬


‫بتبع لوبحيكت آخر ويتم إنشاءه ر‬

‫ه‬
‫ العادية ي‬function ‫ أساسا ال‬،‫ العادية‬function ‫ أساسا هو بينعمل بال‬-
‫ أو نستعمال مشان‬،‫بيصب اذا بدنا نستدعيها‬
‫ر‬ ‫ بس‬،constructer ‫بحد ذاتا‬
‫ بينما‬objects ‫الل بينعملو فيها هن‬‫ ي‬object ‫ ال‬،instances ‫نضيف‬
built-in object ‫ه‬ ‫ بذاتا ي‬function‫ال‬
- function Person(firstName, lastName, age) {
-
- console.log(firstName + lastName + age)
-
- }
-
- console.log(typeof(Person)) ➔ function;
-
- let p1 = new Person1S(`test`,`test`,10);
- console.log(typeof(p1)) ➔ object (even if its not really object
within the oop terminology but all functions (and other built-in
objects are constructers));

another example:

function Person(name){

this.name = name

//methods

this.call = function (){

console.log(this.name)

}
2- Built-in objects/native objects (are actually built-in
functions):

The typeof() for it is ➔ function

Functions, arrays, regular expressions, dates are all built-in


objects,

،constructers ‫ أنهم جميعا‬،built-in objects‫شء يف هذه ال‬


‫أهم أهم ي‬
new Array() ‫ فاستعمال‬،object ‫شء يقومون بإنشاءه هو‬ ‫ أي ي‬،‫بالتال‬
‫ي‬ ‫و‬

typeof ‫ لكن عند محاولة معرفة نوعها باستعمال‬،‫يؤدي إلى إنشاء مصفوفة‬
object ‫سنحصل على‬

:‫أمثلة‬

1- Function Person(){

This.person = `karim`

Const p1 = new Person();

Console.log(typeof(Person)) ➔ function

Console.log(typeof(p1)) ➔ object

2- const arr = [1,2,3]; //this is literal declaration of array

Or const arr = new Array();

‫ وبالتالي‬constructer array ‫بكل األحوال المصفوفة يتم إنشاءها بواسطة ال‬


object ‫فإن المصفوفة هي‬

Console.log(typeof(arr)) ➔ object

Console.log(typeof(arr.constructer)) ➔ function

Console.log(typeof(Array)) ➔ function
Going deeper with primitive and objects, with ‫تلخيص‬
expressions and statements

‫ أخرى‬object ‫ تقوم ببناء‬constructors -1 :‫ هي‬functions ‫كل ال‬

‫ لها هو‬typeof() ‫ هي نوعها‬Array constructor function :‫مثل‬


function

‫ (أو حتى‬new ‫لكن عند التحقق من المصفوفة التي تقوم بإنشائها باستخدام‬
object ‫) فهي ستكون‬literal

‫ فإنها تقوم‬new ‫ التي تقوم عند استعمال‬function Person ‫مثال آخر هو‬
Person ‫ من النوع‬object ‫بإنشاء‬

‫ ) فإنهم‬user-built ‫ أو‬built-in ‫ (سواء كانوا‬constructors ‫ كل ال‬-2


‫) والذي يمكن كل هذه التوابع‬with big F( Function object‫ من ال‬object
objects ‫ من األساس ويسمح لها بإنشاء‬constructors ‫أن تكون‬

You might also like