.ﺪﻨﺘﺴﻫ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ﺮﻫ ﺶﺨﺑ ﻦﯾﺮﺘﻤﻬﻣ ﺎﻫ ﯽﮔﮋﯾﻭ
.ﺪﻨﺘﺴﻫ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ﮏﯾ ﺎﺑ ﻂﺒﺗﺮﻣ ﺮﯾﺩﺎﻘﻣ ﺎﻫ ﯽﮔﮋﯾﻭ
.ﺖﺳﺍ ﺐﺗﺮﻣﺎﻧ ﯼﺎﻫ ﯽﮔﮋﯾﻭ ﺯﺍ ﯼﺍ ﻪﻋﻮﻤﺠﻣ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ﮏﯾ
.ﺪﻨﺘﺴﻫ ﯽﻧﺪﻧﺍﻮﺧ ﻂﻘﻓ ﯽﺧﺮﺑ ﺎﻣﺍ ،ﺩﺮﮐ ﻑﺬﺣ ﻭ ﺩﺮﮐ ﻪﻓﺎﺿﺍ ،ﺩﺍﺩ ﺮﯿﯿﻐﺗ ﺍﺭ ﺎﻫ ﯽﮔﮋﯾﻭ ﻥﺍﻮﺗ ﯽﻣ ﻻًﻮﻤﻌﻣ
:ﺖﺳﺍ ﺮﯾﺯ ﺕﺭﻮﺻ ﻪﺑ ﯽﺷ ﮏﯾ ﯽﮔﮋﯾﻭ ﻪﺑ ﯽﺳﺮﺘﺳﺩ ﯼﺍﺮﺑ ﺲﮑﺘﻨﯿﺳ
objectName.property // person.age
ﺎﯾ
objectName["property"] // person["age"]
ﺎﯾ
objectName[expression] // x = "age"; person[x]
.ﺩﻮﺷ ﯽﺑﺎﯾﺯﺭﺍ ﯽﮔﮋﯾﻭ ﻡﺎﻧ ﻪﺑ ﺪﯾﺎﺑ ﺕﺭﺎﺒﻋ
person.firstname + " is " + person.age + " years old.";
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Properties</h2>
<p>Access a property with .property:</p>
<p id="demo"></p>
<script>
const person = {
firstname: "John",
lastname: "Doe",
age: 50,
eyecolor: "blue"
};
document.getElementById("demo").innerHTML = person.firstname + " is " + person.age + " years old.";
</script>
</body>
</html>
person["firstname"] + " is " + person["age"] + " years old.";
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Properties</h2>
<p>Access a property with ["property"]:</p>
<p id="demo"></p>
<script>
const person = {
firstname: "John",
lastname: "Doe",
age: 50,
eyecolor: "blue"
};
document.getElementById("demo").innerHTML = person["firstname"] + " is " + person["age"] + " years old.";
</script>
</body>
</html>
.ﺪﻧﺯ ﯽﻣ ﻪﻘﻠﺣ ﯽﺷ ﮏﯾ ﺕﺎﯿﺻﻮﺼﺧ ﻖﯾﺮﻃ ﺯﺍ .ﺩﻮﺷ ﯽﻣ ﺍﺮﺟﺍ ﯽﮔﮋﯾﻭ ﺮﻫ ﯼﺍﺮﺑ ﺭﺎﺑ ﮏﯾ :ﯽﺷ ﮏﯾ ﺕﺎﯿﺻﻮﺼﺧ ﻖﯾﺮﻃ ﺯﺍ ﻥﺩﺯ ﻪﻘﻠﺣ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯿﻨﮐ ﻪﻓﺎﺿﺍ ﺪﯾﺪﺟ ﯼﺎﻫ ﯽﮔﮋﯾﻭ ،ﺩﻮﺟﻮﻣ ءﯽﺷ ﮏﯾ ﻪﺑ ﺭﺍﺪﻘﻣ ﮏﯾ ﻥﺩﺍﺩ ﺎﺑ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺎﻤﺷ - ﺖﺳﺍ ﻪﺘﺷﺍﺩ ﺩﻮﺟﻭ ﻞﺒﻗ ﺯﺍ ﺺﺨﺷ ﯽﺷ ﺪﯿﻨﮐ ﺽﺮﻓ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ :ﺪﻨﮐ ﯽﻣ ﻑﺬﺣ ﯽﺷ ﮏﯾ ﺯﺍ ﺍﺭ ﯽﮔﮋﯾﻭ ﮏﯾ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ ;["ﻦﺳ"]ﺺﺨﺷ ﻑﺬﺣ ﺎﯾ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﻨﮐ ﯽﻣ ﻑﺬﺣ ﺍﺭ ﯽﮔﮋﯾﻭ ﺩﻮﺧ ﻢﻫ ﻭ ﯽﮔﮋﯾﻭ ﺵﺯﺭﺍ ﻢﻫ .ﺖﺴﯿﻧ ﻩﺩﺎﻔﺘﺳﺍ ﻞﺑﺎﻗ ﻩﺭﺎﺑﻭﺩ ﻥﺪﺷ ﻪﻓﺎﺿﺍ ﺯﺍ ﻞﺒﻗ ﯽﮔﮋﯾﻭ ،ﻑﺬﺣ ﺯﺍ ﺲﭘ ﺍﺭ ﺩﻮﺷ ﻩﺩﺎﻔﺘﺳﺍ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﺶﯿﭘ ﺯﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ﯼﻭﺭ ﺪﯾﺎﺒﻧ :ﺪﻨﺷﺎﺑ ﯼﺮﮕﯾﺩ ﯽﺷ ﺪﻨﻧﺍﻮﺗ ﯽﻣ ﯽﺷ ﮏﯾ ﺭﺩ ﺩﻮﺟﻮﻣ ﺮﯾﺩﺎﻘﻣ :ﺪﯿﺷﺎﺑ ﻪﺘﺷﺍﺩ ﯽﺳﺮﺘﺳﺩ ﻮﺗ ﺭﺩ ﻮﺗ ءﺎﯿﺷﺍ ﻪﺑ ﺖﮐﺍﺮﺑ ﺖﻣﻼﻋ ﺎﯾ ﻪﻄﻘﻧ ﺖﻣﻼﻋ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ ﺪﯿﻧﺍﻮﺗ ﯽﻣ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ : ﺎﯾ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ : ﺎﯾ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ : ﺎﯾ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ :ﺪﻨﺷﺎﺑ ﺎﯿﺷﺍ ﺪﻨﻧﺍﻮﺗ ﯽﻣ ﺎﻫ ﻪﯾﺍﺭﺁ ﺭﺩ ﺩﻮﺟﻮﻣ ﺮﯾﺩﺎﻘﻣ ﻭ ﺪﻨﺷﺎﺑ ﻪﯾﺍﺭﺁ ﺪﻨﻧﺍﻮﺗ ﯽﻣ ﺎﯿﺷﺍ ﺭﺩ ﺩﻮﺟﻮﻣ :ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻪﯾﺍﺭﺁ ﺮﻫ ﯼﺍﺮﺑ for-in ﻪﻘﻠﺣ ﮏﯾ ﺯﺍ ،ﺎﻫ ﻪﯾﺍﺭﺁ ﻞﺧﺍﺩ ﯼﺎﻫ ﻪﯾﺍﺭﺁ ﻪﺑ ﯽﺳﺮﺘﺳﺩ ﯼﺍﺮﺑ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﻧﺭﺍﺩ ﺰﯿﻧ ﺵﺯﺭﺍ ﮏﯾ ﺎﻬﻧﺁ ﻦﯾﺍ ﺮﺑ ﻩﻭﻼﻋ .ﺪﻧﺭﺍﺩ ﻡﺎﻧ ﮏﯾ ﮎﻼﻣﺍ ﻪﻤﻫ .ﺖﺳﺍ ﯽﯾﺍﺭﺍﺩ ﯼﺎﻫ ﯽﮔﮋﯾﻭ ﺯﺍ ﯽﮑﯾ ﺭﺍﺪﻘﻣ .ﻦﺘﺷﻮﻧ ﻞﺑﺎﻗ ﻭ ﻢﯿﻈﻨﺗ ﻞﺑﺎﻗ ،ﺵﺭﺎﻤﺷ ﻞﺑﺎﻗ :ﺯﺍ ﺪﻨﺗﺭﺎﺒﻋ ﺎﻫ ﯽﮔﮋﯾﻭ ﺮﯾﺎﺳ ﺖﺳﺍ ،؟ﺖﺳﺍ ﻥﺪﻧﺍﻮﺧ ﻞﺑﺎﻗ ﺎﯾﺁ) ﺪﻨﻨﮐ ﯽﻣ ﺺﺨﺸﻣ ﺍﺭ ﯽﮔﮋﯾﻭ ﻪﺑ ﯽﺳﺮﺘﺳﺩ ﻩﻮﺤﻧ ﺎﻫ ﯽﮔﮋﯾﻭ ﻦﯾﺍ ﺪﻧﺍﻮﺗ ﯽﻣ value ﯽﮔﮋﯾﻭ ﻂﻘﻓ ﺎﻣﺍ ،ﺪﻨﺘﺴﻫ ﻥﺪﻧﺍﻮﺧ ﻞﺑﺎﻗ ﺎﻫ ﯽﮔﮋﯾﻭ ﻪﻤﻫ ،ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺭﺩ (ECMAScript 5 ﺖﺳﺍ ﺎﻫ ﯽﮔﮋﯾﻭ ﻡﺎﻤﺗ ﻢﯿﻈﻨﺗ ﻭ ﻦﺘﻓﺮﮔ ﯼﺍﺮﺑ ﯽﯾﺎﻫ ﺵﻭﺭ ﯼﺍﺭﺍﺩ .ﺪﻧﺮﺑ ﯽﻣ ﺙﺭﺍ ﻪﺑ ﺍﺭ ﺩﻮﺧ ﻪﯿﻟﻭﺍ ﻪﻧﻮﻤﻧ ﯼﺎﻫ ﯽﮔﮋﯾﻭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ءﺎﯿﺷﺍ ﮏﯾ ﺎﻤﺷ ﺮﮔﺍ ﺎﻣﺍ ،ﺪﻨﮐ ﯽﻤﻧ ﻑﺬﺣ ﺍﺭ ﯽﺛﺭﺍ ﯼﺎﻫ ﯽﮔﮋﯾﻭ ﻮﺤﻧ
for (let variable in object) {
// code to be executed
}
for...in
ﻪﻘﻠﺣ ﻞﺧﺍﺩ ﻝﺎﺜﻣ
const person = {
fname:" John",
lname:" Doe",
age: 25
};
for (let x in person) {
txt += person[x];
}
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Properties</h2>
<p>Looping object property values:</p>
<p id="demo"></p>
<script>
const person = {
fname:"John",
lname:"Doe",
age:25
};
let txt = "";
for (let x in person) {
txt += person[x] + " ";
}
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
ﺪﯾﺪﺟ ﯼﺎﻫ ﯽﮔﮋﯾﻭ ﻥﺩﻭﺰﻓﺍ
ﻝﺎﺜﻣ
person.nationality = "English";
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Properties</h2>
<p>Add a new property to an existing object:</p>
<p id="demo"></p>
<script>
const person = {
firstname: "John",
lastname: "Doe",
age: 50,
eyecolor: "blue"
};
person.nationality = "English";
document.getElementById("demo").innerHTML =
person.firstname + " is " + person.nationality + ".";
</script>
</body>
</html>
ﺹﺍﻮﺧ ﻑﺬﺣ
ﻑﺬﺣ<"code class="w3-codespan> ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ
ﻝﺎﺜﻣ
const person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
delete person.age;
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Properties</h2>
<p>Deleting object properties.</p>
<p id="demo"></p>
<script>
const person = {
firstname: "John",
lastname: "Doe",
age: 50,
eyecolor: "blue"
};
delete person.age;
document.getElementById("demo").innerHTML =
person.firstname + " is " + person.age + " years old.";
</script>
</body>
</html>
ﻝﺎﺜﻣ
const person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
delete person["age"];
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Properties</h2>
<p>Deleting object properties.</p>
<p id="demo"></p>
<script>
const person = {
firstname: "John",
lastname: "Doe",
age: 50,
eyecolor: "blue"
};
delete person["age"];
document.getElementById("demo").innerHTML =
person.firstname + " is " + person.age + " years old.";
</script>
</body>
</html>
ﻑﺬﺣ<"code class="w3-codespan> ﯼﺪﯿﻠ
ﻑﺬﺣ<"code class="w3-cod
ﻮﺗ ﺭﺩ ﻮﺗ ءﺎﯿﺷﺍ
ﻝﺎﺜﻣ
myObj = {
name:"John",
age:30,
cars: {
car1:"Ford",
car2:"BMW",
car3:"Fiat"
}
}
ﻝﺎﺜﻣ
myObj.cars.car2;
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Objects</h2>
<p>Access nested objects:</p>
<p id="demo"></p>
<script>
const myObj = {
name: "John",
age: 30,
cars: {
car1: "Ford",
car2: "BMW",
car3: "Fiat"
}
}
document.getElementById("demo").innerHTML = myObj.cars.car2;
</script>
</body>
</html>
ﻝﺎﺜﻣ
myObj.cars["car2"];
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Objects</h2>
<p>Access nested objects:</p>
<p id="demo"></p>
<script>
const myObj = {
name: "John",
age: 30,
cars: {
car1: "Ford",
car2: "BMW",
car3: "Fiat"
}
}
document.getElementById("demo").innerHTML = myObj.cars["car2"];
</script>
</body>
</html>
ﻝﺎﺜﻣ
myObj["cars"]["car2"];
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Objects</h2>
<p>Access nested objects:</p>
<p id="demo"></p>
<script>
const myObj = {
name: "John",
age: 30,
cars: {
car1: "Ford",
car2: "BMW",
car3: "Fiat"
}
}
document.getElementById("demo").innerHTML = myObj["cars"]["car2"];
</script>
</body>
</html>
ﻝﺎﺜﻣ
let p1 = "cars";
let p2 = "car2";
myObj[p1][p2];
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Objects</h2>
<p>Access nested objects:</p>
<p id="demo"></p>
<script>
const myObj = {
name: "John",
age: 30,
cars: {
car1: "Ford",
car2: "BMW",
car3: "Fiat"
}
}
let p1 = "cars";
let p2 = "car2";
document.getElementById("demo").innerHTML = myObj[p1][p2];
</script>
</body>
</html>
ﻮﺗ ﺭﺩ ﻮﺗ ءﺎﯿﺷﺍ ﻭ ﺎﻫ ﻪﯾﺍﺭﺁ
ﻝﺎﺜﻣ
const myObj =
{
name: "John",
age: 30,
cars: [
{name:"Ford",
models:["Fiesta", "Focus", "Mustang"]},
{name:"BMW", models:["320", "X3", "X5"]},
{name:"Fiat", models:["500", "Panda"]}
]
}
ﻝﺎﺜﻣ
for (let i in myObj.cars) {
x += "<h1>" + myObj.cars[i].name
+ "</h1>";
for (let j in myObj.cars[i].models) {
x += myObj.cars[i].models[j];
}
}
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>Nested JavaScript Objects and Arrays.</h2>
<p id="demo"></p>
<script>
let x = "";
const myObj = {
name: "John",
age: 30,
cars: [
{name:"Ford", models:["Fiesta", "Focus", "Mustang"]},
{name:"BMW", models:["320", "X3", "X5"]},
{name:"Fiat", models:["500", "Panda"]}
]
}
for (let i in myObj.cars) {
x += "<h2>" + myObj.cars[i].name + "</h2>";
for (let j in myObj.cars[i].models) {
x += myObj.cars[i].models[j] + "<br>";
}
}
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
ﺹﺍﻮﺧ ﯼﺎﻫ ﯽﮔﮋﯾﻭ
ﻪﯿﻟﻭﺍ ﻪﻧﻮﻤﻧ ﯼﺎﻫ ﯽﮔﮋﯾﻭ
ﻑﺬﺣ<"code class="w3-codespan> ﯼﺪ