ECMAScript 2015 ﻡﺎﻧ ﺎﺑ ﻪﮐ ES6 ﺩﺮﮐ ﯽﻓﺮﻌﻣ ﺍﺭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯼﺎﻫ ﺱﻼﮐ ،ﺩﻮﺷ ﯽﻣ ﻪﺘﺧﺎﻨﺷ ﺰﯿﻧ.
.ﺪﻨﺘﺴﻫ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ءﺎﯿﺷﺍ ﯼﺍﺮﺑ ﯽﯾﺎﻫ ﺐﻟﺎﻗ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯼﺎﻫ ﺱﻼﮐ
.ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ class
ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﺯﺍ ﺱﻼﮐ ﺩﺎﺠﯾﺍ ﯼﺍﺮﺑ
:ﺪﯿﻨﮐ ﻪﻓﺎﺿﺍ constructor()
ﻡﺎﻧ ﻪﺑ ﯽﺷﻭﺭ ﻪﺸﯿﻤﻫ
class ClassName {
constructor() { ... }
}
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
}
.ﺪﻨﮐ ﯽﻣ ﺩﺎﺠﯾﺍ "Car" ﻡﺎﻧ ﻪﺑ ﯽﺳﻼﮐ ﻻﺎﺑ ﻝﺎﺜﻣ
."year" ﻭ "name" :ﺖﺳﺍ ﻪﯿﻟﻭﺍ ﯽﮔﮋﯾﻭ ﻭﺩ ﯼﺍﺭﺍﺩ ﺱﻼﮐ
.ﺖﺳﺍ ﺖﺴﯿﻧ ﯽﺷ ﮏﯾ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺱﻼﮐ
.ﺖﺳﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ءﺎﯿﺷﺍ ﯼﺍﺮﺑ ﻮﮕﻟﺍ ﮏﯾ ﻦﯾﺍ
:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ءﺎﯿﺷﺍ ﺩﺎﺠﯾﺍ ﯼﺍﺮﺑ ﺱﻼﮐ ﺯﺍ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ،ﺪﯾﺭﺍﺩ ﺱﻼﮐ ﮏﯾ ﻪﮐ ﯽﻣﺎﮕﻨﻫ
const myCar1 = new Car("Ford", 2014);
const myCar2 = new Car("Audi", 2019);
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Classes</h1>
<p>Creating two car objects from a car class:</p>
<p id="demo"></p>
<script>
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
}
const myCar1 = new Car("Ford", 2014);
const myCar2 = new Car("Audi", 2019);
document.getElementById("demo").innerHTML =
myCar1.name + " " + myCar2.name;
</script>
</body>
</html>
.ﺪﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ Car object ﻭﺩ ﺩﺎﺠﯾﺍ ﯼﺍﺮﺑ Car class ﺯﺍ ﻻﺎﺑ ﻝﺎﺜﻣ
.ﺩﻮﺷ ﯽﻣ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺭﺎﮐﺩﻮﺧ ﺭﻮﻃ ﻪﺑ ﺩﻮﺷ ﯽﻣ ﺩﺎﺠﯾﺍ ﺪﯾﺪﺟ ﯽﺷ ﮏﯾ ﻪﮐ ﯽﻣﺎﮕﻨﻫ ﻩﺪﻧﺯﺎﺳ ﺪﺘﻣ
:ﺖﺳﺍ ﺹﺎﺧ ﺵﻭﺭ ﮏﯾ ﻩﺪﻧﺯﺎﺳ ﺵﻭﺭ
.ﺪﺷﺎﺑ ﻪﺘﺷﺍﺩ ﺍﺭ "ﻩﺪﻧﺯﺎﺳ" ﻖﯿﻗﺩ ﻡﺎﻧ ﺪﯾﺎﺑ
ﺩﻮﺷ ﯽﻣ ﺍﺮﺟﺍ ﺭﺎﮐﺩﻮﺧ ﺭﻮﻃ ﻪﺑ ﺩﻮﺷ ﯽﻣ ﺩﺎﺠﯾﺍ ﺪﯾﺪﺟ ﯽﺷ ﮏﯾ ﻪﮐ ﯽﻣﺎﮕﻨﻫ
ﺩﻮﺷ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﯽﺷ ﺕﺎﯿﺻﻮﺼﺧ ﻪﯿﻟﻭﺍ ﯽﻫﺩﺭﺍﺪﻘﻣ ﯼﺍﺮﺑ
ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ،ﺪﯿﻨﮑﻧ ﻒﯾﺮﻌﺗ ﺍﺭ ﻩﺪﻧﺯﺎﺳ ﺪﺘﻣ ﺮﮔﺍ
.ﺪﻧﻮﺷ ﯽﻣ ﺩﺎﺠﯾﺍ ﯽﺷ ﯼﺎﻫﺪﺘﻣ ﻪﺑﺎﺸﻣ ﺲﮑﺘﻨﯿﺳ ﺎﺑ ﺱﻼﮐ ﯼﺎﻫﺪﺘﻣ
.ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ class
ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﺯﺍ ﺱﻼﮐ ﺩﺎﺠﯾﺍ ﯼﺍﺮﺑ
.ﺪﯿﻨﮐ ﻪﻓﺎﺿﺍ constructor()
ﺵﻭﺭ ﮏﯾ ﻪﺸﯿﻤﻫ
.ﺪﯿﻨﮐ ﻪﻓﺎﺿﺍ ﺍﺭ ﺵﻭﺭ ﺩﺍﺪﻌﺗ ﺮﻫ ﺲﭙﺳ
class ClassName {
constructor() { ... }
method_1() { ... }
method_2() { ... }
method_3() { ... }
}
:ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺍﺭ ﻭﺭﺩﻮﺧ ﻦﺳ ﻪﮐ ﺪﯿﻨﮐ ﺩﺎﺠﯾﺍ "age" ﻡﺎﻧ ﻪﺑ ﺱﻼﮐ ﺪﺘﻣ ﮏﯾ
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
age() {
const date = new Date();
return date.getFullYear() - this.year;
}
}
const myCar = new Car("Ford", 2014);
document.getElementById("demo").innerHTML =
"My car is " + myCar.age() + " years old.";
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Class Methods</h1>
<p>How to define and use a Class method.</p>
<p id="demo"></p>
<script>
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
age() {
const date = new Date();
return date.getFullYear() - this.year;
}
}
const myCar = new Car("Ford", 2014);
document.getElementById("demo").innerHTML =
"My car is " + myCar.age() + " years old.";
</script>
</body>
</html>
:ﺪﯿﻨﮐ ﻝﺎﺳﺭﺍ ﺱﻼﮐ ﯼﺎﻫﺪﺘﻣ ﻪﺑ ﺍﺭ ﺎﻫﺮﺘﻣﺍﺭﺎﭘ ﺪﯿﻧﺍﻮﺗ ﯽﻣ
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
age(x) {
return x - this.year;
}
}
const date = new Date();
let year = date.getFullYear();
const myCar = new
Car("Ford", 2014);
document.getElementById("demo").innerHTML=
"My car is
" + myCar.age(year) + " years old.";
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Class Method</h1>
<p>Pass a parameter into the "age()" method.</p>
<p id="demo"></p>
<script>
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
age(x) {
return x - this.year;
}
}
const date = new Date();
let year = date.getFullYear();
const myCar = new Car("Ford", 2014);
document.getElementById("demo").innerHTML=
"My car is " + myCar.age(year) + " years old.";
</script>
</body>
</html>
ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ ﻥﺁ ﺯﺍ ﻞﻣﺎﮐ ﯽﻧﺎﺒﯿﺘﺸﭘ ﺎﺑ ﺍﺭ ﺮﮔﺭﻭﺮﻣ ﻪﺨﺴﻧ ﻦﯿﻟﻭﺍ ﺮﯾﺯ ﻝﻭﺪﺟ
Chrome 49 | Edge 12 | Firefox 45 | Safari 9 | Opera 36 |
Mar, 2016 | Jul, 2015 | Mar, 2016 | Oct, 2015 | Mar, 2016 |
"use strict"
.ﺩﻮﺷ ﻪﺘﺷﻮﻧ "ﻖﯿﻗﺩ ﺖﻟﺎﺣ" ﺭﺩ ﺪﯾﺎﺑ ﺎﻫ ﺱﻼﮐ ﺭﺩ ﺲﮑﺘﻨﯿﺳ
ﺩﺮﮐ ﺪﯿﻫﺍﻮﺧ ﺖﻓﺎﯾﺭﺩ ﺍﺭ ﮏﯾ ﺎﻤﺷ
ﺪﺷ ﺪﯿﻫﺍﻮﺧ ﻪﺟﺍﻮﻣ ﺎﻄﺧ ﺎﺑ ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻩﺩﺎﻔﺘﺳﺍ ﻥﻭﺪﺑ ﯼﺮﯿﻐﺘﻣ ﺯﺍ ﺮﮔﺍ ﻪﻧﺍﺮﯿﮕﺘﺨﺳ ﺖﻟﺎﺣ ﺭﺩ
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
age() {
// date = new Date(); // This will not work
const date = new Date(); // This will work
return date.getFullYear() - this.year;
}
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Classes uses "strict mode"</h1>
<p>In a JavaScript Class you cannot use variable without declaring it.</p>
<p id="demo"></p>
<script>
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
age() {
// date = new Date(); // This will not work
const date = new Date(); // This will work
return date.getFullYear() - this.year;
}
}
const myCar = new Car("Ford", 2014);
document.getElementById("demo").innerHTML =
"My car is " + myCar.age() + " years old.";
</script>
</body>
</html>
.ﺪﯾﺯﻮﻣﺎﯿﺑ ﺮﺘﺸﯿﺑ JS Strict Mode :ﺭﺩ «ﻪﻧﺍﺮﯿﮕﺘﺨﺳ ﺖﻟﺎﺣ» ﺩﺭﻮﻣ ﺭﺩ