Javascript ! , , , . , , , .
Javascript . , C C++.
:
function test(o) {
var i = 0; // i if (typeof o == "object") {
var j = 0; // j ,
for(var k = 0; k < 10; k++) { // k ,
document.write(k);
}
document.write(k); // k : 10
}
document.write(j); // j ,
}
: ( ). , .. , , .
var x = function(){
var i;
mul = function(b) { return i*b; } for(fact = i = 1; i < 10; i++) {
fact = mul(fact);
document.write(i + "! = " + fact + <br />");
}
}
, , , var.
:
var scope = ""; function f() {
alert(scope); // "". scope = ""; // . alert(scope); // ""
}
f();
alert(scope); // ""
var scope.
var scope = ""; function f() {
alert(scope); // "undefined", "". var scope = ""; // ,
// . alert(scope); // ""
}
f();
alert(scope); // "".
, , var, .
, :
function f1() {
scope = "123"; // alert (scope);
}
function f2() {
alert(scope); //
}
scope
var scope; function f1() {
scope = "123"; // alert(scope);
}
function f2() {
alert(scope);
}
f 2();// "undefined"
f1();// "123"
f 2();// "123"
scope :
|
|
var scope = ""; function f1() {
scope = "123"; // alert(scope);
}
function f2() {
alert(scope);
}
f 2();// "" ( )
f1();// "123"
f 2();// "123"
Javascript - , ++ Java.
-. , . . .
:
// .
// , "this". function Rectangle(w, h) { this.width = w; this.height = h;
}
// Rectangle.
// .
var rect1 = new Rectangle(2, 4); // rect1 = { width:2, height:4 };
var rect2 = new Rectangle(8.5, 11); // rect2 = { width:8.5, height:11 };
Rectangle. this.
:
r.area = function() { return this.width * this.height; }
// ,
var a = r.area();
, :
function Rectangle(w, h) { this.width = w; this.height = h;
this.area = function() { return this.width * this.height; }
}
. (area - , ), . .
. . , -, . prototype -. , , -.
Circle:
//
function Circle(radius) {
// r - , // . this.r = radius;
}
// Circle.PI - ( ), . Circle.PI = 3.14159;
// , .
// C++
Circle.prototype.area = function() { return Circle.PI * this.r * this.r; }
// ( C++)
// Circle . Circle.max = function(a,b) { if (a.r > b.r) return a; else return b;
}
// :
var c = new Circle(1.0); // Circle
c.r = 2.2; // r
var a = c.area(); // area()
var x = Math.exp(Circle.PI); // PI
|
|
var d = new Circle(1.2); // Circle
var bigger = Circle.max(c,d); // max()