Panduan referensi JavaScript global

Fungsi dan properti global JavaScript dapat digunakan untuk semua objek JavaScript bawaan.

Properti global JavaScript

Properti Deskripsi
Infinity Mewakili nilai angka yang tak terbatas (positif/negatif).
NaN Nilai "Tidak adalah bilangan" ("Not-a-Number")
undefined Mewakili variabel belum diset.

Fungsi global JavaScript

Fungsi Deskripsi
decodeURI() Mengurai URI.
decodeURIComponent() Mengurai komponen URI.
encodeURI() Mengkodekan URI.
encodeURIComponent() Mengkodekan komponen URI.
escape() Ditinggalkan dalam versi 1.5.Gunakan encodeURI() atau encodeURIComponent() Gantikan.
eval() Mengukur string dan melaksanakan seperti kode skrip.
isFinite() Mengkonfirmasi apakah nilai adalah bilangan yang terbatas dan sah.
isNaN() Mengkonfirmasi apakah nilai adalah bilangan yang ilegal.
Number() Mengubah nilai objek ke bilangan.
parseFloat() Mengurai string dan mengembalikan bilangan pecahan.
parseInt() Mengurai string dan mengembalikan bilangan bulat.
String() Mengubah nilai objek ke string.
unescape() Ditinggalkan dalam versi 1.5.Gunakan decodeURI() atau decodeURIComponent() Gantikan.

Fungsi atau metode?

Memanggil fungsi global di atas daripada metode global mempunyai makna, karena fungsi adalah pemanggilan global, bukan dari objek mana pun.

Namun demikian, anda juga dapat memanggil metode ini, karena mereka adalah metode objek global lingkungan eksekusi. Dalam pelayar web, objek global adalah jendela pelayar. Jadi isNaN() sebenarnya adalah metode jendela: window.isNaN().

Deskripsi objek global

Objek global adalah objek yang ditetapkan sebelumnya, sebagai penanda untuk fungsi dan properti global JavaScript. Melalui penggunaan objek global, anda dapat mengakses semua objek, fungsi dan properti yang ditetapkan sebelumnya. Objek global bukan properti dari objek apapun, jadi ia tidak memiliki nama.

In the top-level JavaScript code, the keyword this can be used to refer to the global object. But it is usually not necessary to refer to the global object in this way, because the global object is the head of the scope chain, which means that all non-qualified variables and function names will be queried as properties of the object. For example, when JavaScript code references the parseInt() function, it refers to the parseInt property of the global object. The global object is the head of the scope chain, which also means that all variables declared in the top-level JavaScript code will become properties of the global object.

The global object is just an object, not a class. There is no constructor and cannot instantiate a new global object.

When a special environment is embedded in JavaScript code, the global object usually has environment-specific properties. In fact, the ECMAScript standard does not specify the type of the global object, the implementation of JavaScript or the embedded JavaScript can use any type of object as the global object, as long as the object defines the basic properties and functions listed here. For example, in the JavaScript implementation that allows scripting Java through LiveConnect or related technology, the global object is assigned the java and Package properties as well as the getClass() method listed here. And in client-side JavaScript, the global object is the Window object, representing the Web browser window that allows JavaScript code.

Example

In the core language of JavaScript, the predefined properties of the global objects are non-enumerable, all can be listed all implicit or explicit declared global variables with for/in loop, as shown below:

var variables = "";
for (var name in this) 
{
variables += name + "<br />";
}
document.write(variables);

Try It Yourself