[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Guide/Language_overview [Back]  [Original]

JavaScript (JS ) - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

JavaScript (JS )

? JavaScript , . , . JavaScript , , .

. JavaScript 1995 , Netscape. Netscape 2 1996 . LiveScript, , Java Sun Microsystem . .

Microsoft JScript, IE3. Netscape Ecma International, , ECMAScript 1997. ECMAScript edition 3 1999, . , - . ECMAScript 5 ( 2009) ECMAScript 6 ( 2015).

, JavaScript (input) (output). , , . , JavaScript Adobe Acrobat, Photoshop, Yahoo!'s Widget engine, , node.js.

In this article

JavaScript - , , . Java C, JavaScript. JavaScript , ( ES6 Classes) . , , -, .

: . JavaScript , . JavaScript:

  • ( )

, Undefined Null, . , . , . , , . :

. , .

JavaScript "64- IEEE 754", . . JavaScript , , , C Java. :

js
0.1 + 0.2 == 0.30000000000000004;

32- ( ), .

, , , .. , , Math, :

js
Math.sin(3.5);
var circumference = Math.PI * (r + r);

, parseInt(). , :

js
parseInt("123", 10); // 123
parseInt("010", 10); // 10

, :

js
parseInt("010"); // 8
parseInt("0x10"); // 16

, parseInt() - 0, - - "0x".

, :

js
parseInt("11", 2); // 3

, parseFloat(), 10 parseInt().

+ :

js
+"42"; // 42
+"0x10"; // 16

NaN ( "Not a Number") , :

js
parseInt("hello", 10); // NaN

NaN "": NaN NaN:

js
NaN + 5; // NaN

NaN isNaN():

js
isNaN(NaN); // true

JavaScript Infinity () -Infinity:

js
1 / 0; // Infinity
-1 / 0; // -Infinity

Infinity, -Infinity NaN isFinite():

js
isFinite(1 / 0); // false
isFinite(-Infinity); // false
isFinite(NaN); // false

JavaScript - Unicode ( UTF-16). , , . , - , 16- , Unicode 1 2 .

, , .

( ), length:

js
"hello".length; // 5

! , ? :

js
"hello".charAt(0); // h
"hello, world".replace("hello", "goodbye"); // goodbye, world
"hello".toUpperCase(); // HELLO

JavaScript , null, , undefined, , , . , JavaScript . "undefined".

JavaScript () , true false ( ). :

  1. false, 0, (""), NaN, null undefined false.
  2. true.

, Boolean():

js
Boolean(""); // false
Boolean(234); // true

, JavaScript , , if. - , , , "" "".

: && ( ), || ( ), ! ( ).

JavaScript let, const var.

js
let a;
let name = "Simon";

let , , :

js
// myLetVariable  

for (let myLetVariable = 0; myLetVariable < 5; myLetVariable++) {
  // myLetVariable   
}

// myLetVariable  

const , . , .

js
const Pi = 3.14; //   Pi  .
Pi = 1; //  ,      .

var . , . , JavaScript. , var, , .

js
var a;
var name = "Simon";

, var:

js
// myVarVariable  

for (var myVarVariable = 0; myVarVariable < 5; myVarVariable++) {
  // myVarVariable    
}

// myVarVariable   

- , undefined.

JavaScript , , . , var (, if), . , ECMAScript Edition 6 let const, , .

JavaScript , +, -, *, / %, ( ). =, += -=. x = x y.

js
x += 5;
x = x + 5;

(++) (--). .

+ () :

js
"hello" + " world"; // "hello world"

. :

js
"3" + 4 + 5; // "345"
3 + 4 + "5"; // "75"

.

JavaScript : <, >, <= >=. , . . (==) (===) . == , :

js
123 == "123"; // true
1 == true; // true

, :

js
1 === true; // false
123 === "123"; // false
true === true; // true

!= !==.

, .

JavaScript C. if else, :

js
var name = "kittens";
if (name == "puppies") {
  name += "!";
} else if (name == "kittens") {
  name += "!!";
} else {
  name = "!" + name;
}
name == "kittens!!";

JavaScript : while, do-while for. While , do-while , , :

js
while (true) {
  //  !
}

var input;
do {
  input = get_input();
} while (inputIsNotValid(input));

for C Java: :

js
for (var i = 0; i < 5; i++) {
  //  5 
}

JavaScript : for...of

js
for (let value of array) {
  //   value
}

for...in:

js
for (let property in object) {
  //    
}

&& || " ", , . , , :

js
var name = o && o.getName();

:

js
var name = otherName || "default";

JavaScript "?" :

js
var allowed = age > 18 ? "yes" : "no";

switch :

js
switch (action) {
  case "draw":
    drawit();
    break;
  case "eat":
    eatit();
    break;
  default:
    donothing();
}

case break, case. , , , :

js
switch (a) {
  case 1: // fallthrough
  case 2:
    eatit();
    break;
  default:
    donothing();
}

default . switch, cases. ===:

js
switch (1 + 3) {
  case 2 + 2:
    yay();
    break;
  default:
    neverhappens();
}

JavaScript - (-). :

  • Python.
  • Perl Ruby.
  • C C++.
  • HashMaps Java.
  • PHP.

JavaScript , JavaScript, . .

:

js
var obj = new Object();

:

js
var obj = {};

. . JSON, .

, :

js
var obj = {
  name: "Carrot",
  for: "Max",
  details: {
    color: "orange",
    size: 12,
  },
};

:

js
obj.details.color; // orange
obj["details"]["size"]; // 12

. , . , . , . , :

js
//  Syntax error,  'for'   
obj.for = "Simon";

//    
obj["for"] = "Simon";

: Object.prototype.

Inheritance and the prototype chain.

JavaScript . ( , []), length (). , + 1.

:

js
var a = new Array();
a[0] = "dog";
a[1] = "cat";
a[2] = "hen";
a.length; // 3

:

js
var a = ["dog", "cat", "hen"];
a.length; // 3

, array.length . :

js
var a = ["dog", "cat", "hen"];
a[100] = "fox";
a.length; // 101

.

, undefined:

js
typeof a[90]; // undefined

:

js
for (var i = 0; i < a.length; i++) {
  //  -   a[i]
}

ES2015 for...of , .. :

for (const currentValue of a) {
  //  -  currentValue
}

for...in. , - Array.prototype, . .

ECMAScript 5 forEach():

js
["dog", "cat", "hen"].forEach(function (currentValue, index, array) {
  //  -  currentValue  array[index]
});

push():

js
a.push(item);

. .

a.toString() , .
a.toLocaleString() .
a.concat(item1[, item2[, ...[, itemN]]]) .
a.join(sep) , sep
a.pop() .
a.push(item1, ..., itemN) .
a.reverse() .
a.shift() .
a.slice(start[, end]) .
a.sort([cmpfn]) .
a.splice(start, delcount[, item1[, ...[, itemN]]]) .
a.unshift(item1[, item2[, ...[, itemN]]]) .

JavaScript. :

js
function add(x, y) {
  var total = x + y;
  return total;
}

, . JavaScript . , . return . return ( , ), JavaScript undefined.

, . , undefined:

js
add(); // NaN
//    undefined  undefined

, :

js
add(2, 3, 4); // 5
//     , "4" 

, "" arguments, , . , :

js
function add() {
  var sum = 0;
  for (var i = 0, j = arguments.length; i < j; i++) {
    sum += arguments[i];
  }
  return sum;
}

add(2, 3, 4, 5); // 14

:

js
function avg() {
  var sum = 0;
  for (var i = 0, j = arguments.length; i < j; i++) {
    sum += arguments[i];
  }
  return sum / arguments.length;
}
avg(2, 3, 4, 5); // 3.5

, . . , . : ...variable , . for for...of , .

js
function avg(...args) {
  var sum = 0;
  for (let value of args) {
    sum += value;
  }
  return sum / args.length;
}

avg(2, 3, 4, 5); // 3.5

JavaScript :

js
var avg = function () {
  var sum = 0;
  for (var i = 0, j = arguments.length; i < j; i++) {
    sum += arguments[i];
  }
  return sum / arguments.length;
};

function avg(). . , "" :

js
var a = 1;
var b = 2;
(function () {
  var b = 3;
  a += b;
})();
a; // 4
b; // 2

JavaScript . () ( , DOM).

js
function countChars(elm) {
  if (elm.nodeType == 3) {
    // TEXT_NODE
    return elm.nodeValue.length;
  }
  var count = 0;
  for (var i = 0, child; (child = elm.childNodes[i]); i++) {
    count += countChars(child);
  }
  return count;
}

: , ? JavaScript IIFEs (Immediately Invoked Function Expressions). :

js
var charsInBody = (function counter(elm) {
  if (elm.nodeType == 3) {
    // TEXT_NODE
    return elm.nodeValue.length;
  }
  var count = 0;
  for (var i = 0, child; (child = elm.childNodes[i]); i++) {
    count += counter(child);
  }
  return count;
})(document.body);

. .

- () , . JavaScript - , , , , C++ Java. ( , , .) JavaScript . , . : " " ", ". :

js
function makePerson(first, last) {
  return {
    first: first,
    last: last,
  };
}

function personFullName(person) {
  return person.first + " " + person.last;
}

function personFullNameReversed(person) {
  return person.last + ", " + person.first;
}

s = makePerson("Simon", "Willison");
personFullName(s); // Simon Willison
personFullNameReversed(s); // Willison, Simon

, . , . , . , :

js
function makePerson(first, last) {
  return {
    first: first,
    last: last,
    fullName: function () {
      return this.first + " " + this.last;
    },
    fullNameReversed: function () {
      return this.last + ", " + this.first;
    },
  };
}
s = makePerson("Simon", "Willison");
s.fullName(); // Simon Willison
s.fullNameReversed(); // Willison, Simon

- : this. this , . . , this . this . . :

js
s = makePerson("Simon", "Willison");
var fullName = s.fullName;
fullName(); // undefined undefined

fullName(), this . first last, undefined.

this, makePerson:

js
function Person(first, last) {
  this.first = first;
  this.last = last;
  this.fullName = function () {
    return this.first + " " + this.last;
  };
  this.fullNameReversed = function () {
    return this.last + ", " + this.first;
  };
}
var s = new Person("Simon", "Willison");

: new. this. , , this . , new . , - .

, fullName().

, , . :

js
function personFullName() {
  return this.first + " " + this.last;
}
function personFullNameReversed() {
  return this.last + ", " + this.first;
}
function Person(first, last) {
  this.first = first;
  this.last = last;
  this.fullName = personFullName;
  this.fullNameReversed = personFullNameReversed;
}

: - , - . ? :

js
function Person(first, last) {
  this.first = first;
  this.last = last;
}
Person.prototype.fullName = function fullName() {
  return this.first + " " + this.last;
};
Person.prototype.fullNameReversed = function fullNameReversed() {
  return this.last + ", " + this.first;
};

Person.prototype , Person. . , Person, JavaScript , Person.prototype. , Person.prototype, this .

. JavaScript , , :

js
s = new Person("Simon", "Willison");
s.firstNameCaps();
// TypeError on line 1: s.firstNameCaps is not a function

Person.prototype.firstNameCaps = function () {
  return this.first.toUpperCase();
};
s.firstNameCaps(); // "SIMON"

, JavaScript. reversed String, :

js
var s = "Simon";
s.reversed(); // TypeError on line 1: s.reversed is not a function

String.prototype.reversed = function reversed() {
  var r = "";
  for (var i = this.length - 1; i >= 0; i--) {
    r += this[i];
  }
  return r;
};
s.reversed(); // "nomiS"

!

js
"This can now be reversed".reversed();
// desrever eb won nac sihT

, prototype . Object.prototype, toString() , , . Person:

js
var s = new Person("Simon", "Willison");
s.toString(); // [object Object]

Person.prototype.toString = function () {
  return "<Person: " + this.fullName() + ">";
};

s.toString(); // "<Person: Simon Willison>"

, avg.apply() null? : , apply() , this. new:

js
function trivialNew(constructor, ...args) {
  var o = {}; //   
  constructor.apply(o, args);
  return o;
}

new, ( ). apply() , . , ...args ( ) , .

js
var bill = trivialNew(Person, "William", "Orange");

:

js
var bill = new Person("William", "Orange");

JavaScript apply() call(), this, , .

js
function lastNameCaps() {
  return this.last.toUpperCase();
}
var s = new Person("Simon", "Willison");
lastNameCaps.call(s);
//  :
s.lastNameCaps = lastNameCaps;
s.lastNameCaps(); // WILLISON

. , makePerson(). , , -:

js
function parentFunc() {
  var a = 1;

  function nestedFunc() {
    var b = 4; // parentFunc can't use this
    return a + b;
  }
  return nestedFunc(); // 5
}

, . , , . , .

. , . , . -, ( ), .

(Closures)

JavaScript. .

js
function makeAdder(a) {
  return function (b) {
    return a + b;
  };
}

var x = makeAdder(5);
var y = makeAdder(20);
x(6); // ?
y(7); // ?

makeAdder , , .

, , . . , . . "" makeAdder, ( , - 5, - 20). :

js
x(6); //  11
y(7); //  27

: JavaScript , 'scope', , . , . 'Scope' , , : 'scope' , , 'scope' . .

makeAdder 'scope' : a, , . makeAdder . ' ' scope, . scope , .

scope , JavaScript.

scope, . .


Web Proxy Viewer  |  New URL  |  Original Page