انتقل إلى محتوى الدرس

Constant Objects can Change يمكن تغيير قيم الكائنات الثابته في الجافاسكريب

الكائن عبارة عن مجموعة من الخصائص ، والخاصية تتكون من name ويكون من النوع string و key ويمكن ان يكون من أي نوع بيانات . يمكن أن تكون قيمة الخاصية دالة ، وفي هذه الحالة تُعرف الخاصية ب Method. بالإضافة إلى الكائنات المحددة مسبقًا في المتصفح ،

ستتعلم في هذا الدرس الكائنات Objects وخصائصها properties وال Methods وال Syntax الخاص بال Objects

1 صفوف تعليمية
جافا سكريبت
4,989 مشاهدة

شرح صفوف ومحتوى الفقرة

الصف 1 شرح توضيحي

يختلف ال object عن المتغير العادي لأن ال object نستطيع تغيير خصائصه object properties اذا كان من النوع  constant وذلك علي عكس المتغيرات 

Right Way

HTML
const student = {name:"Mohamed", age:26, country:"Egypt"};
student.id= 27;
student.id = 17;
document.write('Student id' + student.id ) ;

Error

HTML
const student = {name:"Mohamed", age:26, country:"Egypt"};
student = {name:"Mohamed", age:27, country:"Egypt",id:17};
document.write('Student age ' + student.age) ;

</> Unchangeable object property
خصائص لا يمكن تغييرها في كائنات الجافاسكريبت

نستطيع تثبيت قيمة أحد خصائص الكائنات objects ومنع تغيير قيمتها باستخدام دالة تسمي 
( )object.freeze 

في المثال التالي قمنا بوضع الخاصية "name" بداخل دالة ( )object.freeze وبهذه الطريقة لا يمكننا أعادة تعيين قيمة هذه الخاصية

HTML
const company = Object.freeze({
    name: 'closetag',
    address: {
        street:' Abbas alaaqad ',
        city: 'Nasr city',
        state: 'Cairo',
        zipcode: 11765
    }
});

مع استخدام دالة ( )object.freeze

HTML
const company = Object.freeze({
    name: 'closetag',
    address: {
        street: 'Abbas alaaqad ',
        city: 'Nasr city',
        state: 'Cairo',
        zipcode: 11765
    }
});
company.name = " Google"
document.write("the company name is " + company.name) ;

بدون استخدام دالة ( )object.freeze

HTML
const company = {
    name: 'closetag',
    address: {
        street: 'Abbas alaaqad ',
        city: 'Nasr city',
        state: 'Cairo',
        zipcode: 11765
    }
};
company.name = " Google"
document.write("the company name is " + company.name) ;