Aplikasi Objek ECMAScript
- Previous Page Object-Oriented
- Next Page Object Types
Pembuatan dan penghapusan objek terjadi selama eksekusi JavaScript, memahami makna paradigma ini penting bagi memahami seluruh bahasa.
Pengumuman dan Instansiasi
Cara membuat objek adalah dengan kata kunci new diikuti dengan nama kelas yang diinstansiasi:
var oObject = new Object(); var oStringObject = new String();
Baris pertama kode membuat instance kelas Object dan menyimpannya ke variabel oObject. Baris kedua kode membuat instance kelas String dan menyimpannya ke variabel oStringObject. Jika fungsi konstruktur tidak memiliki parameter, tanda kurung tidak diperlukan. Oleh karena itu, kode di atas dapat ditulis kembali seperti berikut:
var oObject = new Object; var oStringObject = new String;
Referensi Objek
Dalam bab sebelumnya, kami menjelaskanKonsep Tipe ReferensiDalam ECMAScript, kita tidak dapat mengakses ekspresi fisik objek, tapi hanya mengakses referensi objek. Setiap kali objek dibuat, yang disimpan di variabel adalah referensi objek tersebut, bukan objek itu sendiri.
Penghapusan Objek
ECMAScript memiliki routine pengumpulan sampah (garbage collection routine), yang berarti tidak perlu menghancurkan objek khusus untuk melepaskan memori. Saat tidak ada referensi lagi untuk objek, disebutkan bahwa objek dihapus (dereference). Saat program pengumpulan sampah dijalankan, semua objek yang dihapus akan dihancurkan. Setiap kali fungsi melaksanakan kode-nya, program pengumpulan sampah akan berjalan, melepaskan semua variabel lokal, dan dalam beberapa situasi yang tak diperkirakan lainnya, program pengumpulan sampah juga akan berjalan.
Setting all references to an object to null can forcibly废除 the object. For example:
var oObject = new Object; // do something with the object here oObject = null;
When the variable oObject is set to null, the reference to the first created object does not exist. This means that when the garbage collection program runs next time, the object will be destroyed.
After using an object, it is a good habit to废除 it to release memory. This also ensures that objects that are no longer accessible are no longer used, thus preventing the occurrence of programming errors. In addition, old browsers (such as IE/MAC) do not have a comprehensive garbage collection program, so objects may not be destroyed correctly when the page is unloaded.废除ing the object and all its properties is the best way to ensure correct memory usage.
Note:Be careful when废除 all references to an object. If an object has two or more references, to correctly废除 the object, all references must be set to null.
Early Binding and Late Binding
Binding, in a sense, is the method of combining an object's interface with an object instance.
Early binding refers to defining the properties and methods of an object before instantiation, so that the compiler or interpreter can convert machine code in advance. With early binding, such as in languages like Java and Visual Basic, IntelliSense (which provides a list of properties and methods in the object to the developer) can be used in the development environment. ECMAScript is not a strongly typed language, so it does not support early binding.
On the other hand, late binding refers to the compiler or interpreter not knowing the type of the object before runtime. With late binding, there is no need to check the type of the object, just check if the object supports properties and methods. All variables in ECMAScript use late binding methods. This allows for a large number of object operations without any penalty.
- Previous Page Object-Oriented
- Next Page Object Types