(, , ) . , , , . . , , :
function passByVal(arg1) {
var arg1 = 10;
}
var x = 5;
passByVal(5);
alert(x);
// 5,
function passByRef(arr1) {
arr1[0] = "NEW";
}
var y = ["first", "second"];
passByRef(y);
alert(y);
// NEW, second, ,
JavaScript : , , , . . , var .
window. , , window, undefined.
function myFunc() {
var x = "local";
y = "global";
}
myFunc();
alert(window.x);
// undefined
alert(y);
// global
alert(x);
//
JavaScript :
, Object;
JavaScript String ; Array ; Date ; Math ; Object ;
:
window ;
document , HTML-;
location , , URL;
navigator ;
history , ;
, HTML CSS JavaScript HTML CSS document, ;
, . . :
function Book() {
}
book :
|
|
var myBook = new Book();
, this. :
function Book() {
this.paper = true;
}
var myBook = new Book();
alert(myBook.paper);
// true, .. Book paper true
, .
function Book(isPaper) {
if (isPaper) this.paper = true;
else this.paper = false;
}
var myBook = new Book(false);
alert(myBook.paper);
// false
prototype, . :
Book.prototype.paper = false;
Book.prototype.isPaperBook = function() {
if (this.paper) alert("This is a paper book");
else alert("This is not a paper book");
}
function Book(isPaper) {
if (isPaper) this.paper = true;
}
var myBook = new Book(true);
myBook.isPaperBook();
// This is a paper book
, , paper false. , true.
- JavaScript
JavaScript - ( -) , .
:
var h = new Object();
h.property = 10;
alert(h.property);
, , 110:
var h = new Object();
h.property = 10;
h[0] = 100;
alert(h.property + h[0]);
, , h.property h[0] .
:
var h = new Object();
h.property = 10;
var s = 'ty';
alert(h['proper' + s]);
h['property'] , h.property.
delete:
var h = new Object();
h['property'] = 10;
alert(delete h['property']);
// true
alert(h['property']);
// undefined
:
var h = new Object();
...
var props = '';
for(var i in h)
props += i + ': ' + h[i] + '\n';
alert(props);
, i ,
.