آشنایی با مفاهیم شیء گرایی در جاوا اسکریپت #1
نویسنده: مهران ر
تاریخ: ۱۳۹۲/۱۰/۰۴ ۱۴:۳۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<script type=”text/javascript”> var person = new Object(); person.firstname = “John”; person.lastname = “Doe”; person.age = 50; person.eyecolor = “blue”; document.write(person.firstname + “ is “ + person.age + “ years old.”); </script>
result : John is 50 years old.
<script type=”text/javascript”>
var obj = {
var1: “text1”,
var2: 5,
Method: function ()
{
alert(this.var1);
}
};
obj.Method();
</script> Result : text1
Obj.var3 = “text3”;
var newobj = obj; newobj.var1 = "other text"; alert(obj.var1);// other text alert(newobj.var1);// other text
<script type=”text/javascript”>
function Person(firstname, lastname, age, eyecolor)
{
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
this.eyecolor = eyecolor;
}
var myFather = new Person("John", "Doe", 50, "blue");
document.write(myFather.firstname + " is " + myFather.age + " years old.");
result : John is 50 years old.
var myMother=new person("Sally","Rally",48,"green");
document.write(myMother.firstname + " is " + myFather.age + " years old.");
result : Sally is 48 years old.
</script> var Person = function (firstname, lastname, age, eyecolor)
{
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
this.eyecolor = eyecolor;
}
var myFather = new Person("John", "Doe", 50, "blue");
document.write(myFather.firstname + " is " + myFather.age + " years old.");
result : John is 50 years old.
var myMother=new person("Sally","Rally",48,"green");
document.write(myMother.firstname + " is " + myFather.age + " years old.");
result : Sally is 48 years old. <script type="text/javascript">
function cat(name) {
this.name = name;
this.talk = function() {
alert( this.name + " say meeow!" )
}
}
cat1 = new cat("felix")
cat1.talk() //alerts "felix says meeow!"
cat2 = new cat("ginger")
cat2.talk() //alerts "ginger says meeow!"
</Script> var myObjectLiteral = {
property1: "one",
property2: "two",
method1: function() {
alert("Hello world!");
}}
var myChild = Object.create(myObjectLiteral);
myChild.method1(); // will alert "Hello world!" function User (theName, theEmail) {
this.name = theName;
this.email = theEmail;
this.quizScores = [];
this.currentScore = 0;
} User.prototype = {
saveScore:function (theScoreToAdd) {
this.quizScores.push(theScoreToAdd)
},
showNameAndScores:function () {
var scores = this.quizScores.length > 0 ? this.quizScores.join(",") : "No Scores Yet";
return this.name + " Scores: " + scores;
},
changeEmail:function (newEmail) {
this.email = newEmail;
return "New Email Saved: " + this.email;
}
} // A User
firstUser = new User("Richard", "Richard@examnple.com");
firstUser.changeEmail("RichardB@examnple.com");
firstUser.saveScore(15);
firstUser.saveScore(10);
document.write(firstUser.showNameAndScores()); //Richard Scores: 15,10
document.write('<br/>');
// Another User
secondUser = new User("Peter", "Peter@examnple.com");
secondUser.saveScore(18);
document.write(secondUser.showNameAndScores()); //Peter Scores: 18 <script type="text/javascript">
function Base()
{
this.color = "blue";
}
function Sub()
{
}
Sub.prototype = new Base();
Sub.prototype.showColor = function ()
{
alert(this.color);
}
var instance = new Sub();
instance.showColor(); //"blue"
</Script> <script language="javascript" type="text/javascript">
if (typeof Object.create !== 'function') {
Object.create = function (o) {
ایجاد یک کلاس خالی که قرار است خواص کلاس دریافتی توسط آرگومان کلاس پایه را به ارث ببرد//
function F() {
}
با ارث برده شود F باعث میشویم کلیه خواص و متدهای دریافتی توسط Prototype توسط خصوصیت F با انتساب آرگومان دریافتی که یک شی است به کلاس
F.prototype = o;
return new F();
};
}
var cars = {
type: "sedan",
wheels: 4
};
// We want to inherit from the cars object, so we do:
var toyota = Object.create(cars);
// now toyota inherits the properties from cars
document.write(toyota.type);
</script>
output :sedan