ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺒﻧﺎﺟ ﻡﺯﺍﻮﻟ


فهرست مطالب

    نمایش فهرست مطالب


(Setter ﻭ Getters) ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺒﻧﺎﺟ ﻡﺯﺍﻮﻟ

ECMAScript 5 (ES5 2009) Getter ﻭ Setters ﺩﺮﮐ ﯽﻓﺮﻌﻣ ﺍﺭ.

Object Accessors (Computed ﻪﮐ ﺪﻨﻫﺩﯽﻣ ﺍﺭ ﻥﺎﮑﻣﺍ ﻦﯾﺍ ﺎﻤﺷ ﻪﺑ ﺎﻫﻩﺪﻨﻨﮐﻢﯿﻈﻨﺗ


(ﺖﻓﺎﯾﺭﺩ ﯼﺪﯿﻠﮐ ﺪﯿﻠﮐ) ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻩﺪﻨﻨﮐ ﺖﻓﺎﯾﺭﺩ

.ﺪﻨﮐﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﺖﻓﺎﯾﺭﺩ<"code class="w3-codespan> ﯼﺍﺮﺑ

ﻝﺎﺜﻣ

// Create an object:
const person = {
  firstName: "John",
  lastName: "Doe",
  language: "en",
  get lang() {
    return this.language;
    }
};

// Display data from the object using a getter:
document.getElementById("demo").innerHTML = person.lang;

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Getters and Setters</h2>

<p>Getters and setters allow you to get and set object properties via methods.</p>

<p>This example uses a lang property to get the value of the language property:</p>

<p id="demo"></p>

<script>
// Create an object:
const person = {
  firstName: "John",
  lastName: "Doe",
  language: "en",
  get lang() {
    return this.language;
  }
};

// Display data from the object using a getter:
document.getElementById("demo").innerHTML = person.lang;
</script>

</body>
</html>

(ﻪﻋﻮﻤﺠﻣ ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ) ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻩﺪﻨﻨﮐ ﻢﯿﻈﻨﺗ

.ﺪﻨﮐﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﻢﯿﻈﻨﺗ<"code class="w3-codespan> ﯼﺍﺮﺑ

ﻝﺎﺜﻣ

const person = {
  firstName: "John",
  lastName: "Doe",
  language: "",
    set lang(lang) {
    this.language = lang;
    }
};

// Set an object 
property using a setter:
person.lang = "en";

// Display data from the object:
document.getElementById("demo").innerHTML = person.language;

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Getters and Setters</h2>

<p>Getters and setters allow you to get and set properties via methods.</p>

<p>This example uses a lang property to set the value of the language property.</p>

<p id="demo"></p>

<script>
// Create an object:
const person = {
  firstName: "John",
  lastName: "Doe",
  language: "NO",
  set lang(value) {
    this.language = value;
  }
};

// Set a property using set:
person.lang = "en";

// Display data from the object:
document.getElementById("demo").innerHTML = person.language;
</script>

</body>
</html>


؟ﻩﺪﻨﻨﮐ ﺖﻓﺎﯾﺭﺩ ﺎﯾ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺎﺗ

؟ﺖﺴﯿﭼ ﻝﺎﺜﻣ ﻭﺩ ﻦﯾﺍ ﺕﻭﺎﻔﺗ

1 ﻝﺎﺜﻣ

const person = {
  firstName: "John",
  lastName: "Doe",
  fullName: function() {
    return this.firstName + " " + 
this.lastName;
  }
};
// Display data from the object using a method:
  document.getElementById("demo").innerHTML = person.fullName();

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Methods</h2>

<p>Object data can be accessed using property stored as a function.</p>

<p id="demo"></p>

<script>
// Create an object:
const person = {
  firstName: "John",
  lastName: "Doe",
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
};

// Display data from the object using a method:
document.getElementById("demo").innerHTML = person.fullName();
</script>

</body>
</html>

2 ﻝﺎﺜﻣ

const person = {
  firstName: "John",
  lastName: "Doe",
  get fullName() {
    return this.firstName + " " + 
this.lastName;
  }
};
// Display data from the object using a getter:
  document.getElementById("demo").innerHTML = person.fullName;

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Methods</h2>

<p>Object data can be accessed using a getter.</p>

<p id="demo"></p>

<script>
// Create an object:
const person = {
  firstName: "John",
  lastName: "Doe",
  get fullName() {
    return this.firstName + " " + this.lastName;
  }
};
// Display data from the object using a getter:
document.getElementById("demo").innerHTML = person.fullName;
</script>

</body>
</html>

.()person.fullName :ﺪﯿﻨﮐ ﺍﺪﯿﭘ ﯽﺳﺮﺘﺳﺩ ﻊﺑﺎﺗ ﮏﯾ ﻥﺍﻮﻨﻋ ﻪﺑ fullName ﻪﺑ 1 ﻝﺎﺜﻣ

.person.fullName :ﺪﯿﻨﮐ ﺍﺪﯿﭘ ﯽﺳﺮﺘﺳﺩ fullName ﻪﺑ ﯽﮔﮋﯾﻭ ﮏﯾ ﻥﺍﻮﻨﻋ ﻪﺑ 2 ﻝﺎﺜﻣ

.ﺪﻫﺩ ﯽﻣ ﻪﺋﺍﺭﺍ ﯼﺮﺗ ﻩﺩﺎﺳ ﻮﺤﻧ ﮏﯾ ﻡﻭﺩ ﻝﺎﺜﻣ


ﻩﺩﺍﺩ ﺖﯿﻔﯿﮐ

.ﺪﻨﮐ ﻦﯿﻤﻀﺗ ﺎﻫ ﻩﺪﻨﻨﮐ ﻢﯿﻈﻨﺗ ﻭ ﺎﻫ ﻩﺪﻧﺮﯿﮔ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﻡﺎﮕﻨﻫ ﺍﺭ ﯼﺮﺘﻬﺑ ﯼﺎﻫ ﻩﺩﺍﺩ ﺖﯿﻔﯿﮐ ﺪﻧﺍﻮﺗ

.ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺍﺭ ﺭﺍﺪﻘﻣ ،ﻝﺎﺜﻣ ﻦﯾﺍ ﺭﺩ ،lang ﯽﮔﮋﯾﻭ ﺯﺍ ﻩﺩ

ﻝﺎﺜﻣ

// Create an object:
const person = {
  firstName: "John",
  lastName: "Doe",
  language: "en",
  get lang() {
    return this.language.toUpperCase();
    }
};

// Display data from the object using a getter:
document.getElementById("demo").innerHTML = person.lang;

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Getters and Setters</h2>

<p>Getters and setters allow you to get and set properties via methods.</p>

<p id="demo"></p>

<script>
// Create an object:
const person = {
  firstName: "John",
  lastName: "Doe",
  language: "en",
  get lang() {
    return this.language.toUpperCase();
  }
};
// Display data from the object using a getter:
document.getElementById("demo").innerHTML = person.lang;
</script>

</body>
</html>

.ﺩﻮﺷﯽﻣ ﻩﺮﯿﺧﺫ ﮒﺭﺰﺑ ﻑﻭﺮﺣ ،ﻝﺎﺜﻣ ﻦﯾﺍ ﺭﺩ ،lang ﯽﮔﮋﯾﻭ

ﻝﺎﺜﻣ

const person = {
  firstName: "John",
  lastName: "Doe",
  language: "",
  set lang(lang) {
      this.language = lang.toUpperCase();
    }
};

// Set an object 
property using a setter:
person.lang = "en";

// Display data from the object:
document.getElementById("demo").innerHTML = person.language;

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Getters and Setters</h2>

<p>Getters and setters allow you to get and set properties via methods.</p>

<p id="demo"></p>

<script>
// Create an object:
const person = {
  firstName: "John",
  lastName: "Doe",
  language: "",
  set lang(lang) {
    this.language = lang.toUpperCase();
  }
};

// Set a property using set:
person.lang = "en";

// Display data from the object:
document.getElementById("demo").innerHTML = person.language;
</script>

</body>
</html>

؟ﻢﯿﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ Setter ﻭ Getters ﺯﺍ ﺍﺮﭼ


Object.defineProperty()

ﻭ Getters ﻥﺩﻭﺰﻓﺍ ﯼﺍﺮﺑ ﺪﻧﺍﻮﺗ ﯽﻣ ﻦﯿﻨﭽﻤﻫ Object.defineProperty

ﻞﺑﺎﻘﺘﻣ ﻝﺎﺜﻣ ﮏﯾ

// Define object
const obj = {counter : 0};

// Define setters and getters
Object.defineProperty(obj, "reset", {
  get : function () {this.counter = 0;}
});
Object.defineProperty(obj, "increment", {
  get : function () {this.counter++;}
});
Object.defineProperty(obj, "decrement", {
  get : function () {this.counter--;}
});
Object.defineProperty(obj, "add", {
  set : function (value) {this.counter += value;}
});
Object.defineProperty(obj, "subtract", {
  set : function (value) {this.counter -= value;}
});

// Play with the counter:
obj.reset;
obj.add = 5;
obj.subtract = 1;
obj.increment;
obj.decrement;

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Getters and Setters</h2>

<p>Perfect for creating counters:</p>

<p id="demo"></p>

<script>
// Define an object
const obj = {counter : 0};

// Define Setters and Getters
Object.defineProperty(obj, "reset", {
  get : function () {this.counter = 0;}
});
Object.defineProperty(obj, "increment", {
  get : function () {this.counter++;}
});
Object.defineProperty(obj, "decrement", {
  get : function () {this.counter--;}
});
Object.defineProperty(obj, "add", {
  set : function (value) {this.counter += value;}
});
Object.defineProperty(obj, "subtract", {
  set : function (value) {this.counter -= value;}
});

// Play with counter:
obj.reset;
obj.add = 5;
obj.subtract = 1;
obj.increment;
obj.decrement;
document.getElementById("demo").innerHTML = obj.counter;
</script>

</body>
</html>

تمامی حقوق محفوظ است. © BasicIT.org • 2023-2024