Repositório contendo estudos de javascript. Estruturas de dados e algoritmos complementáres.
- Objetos Globais
- Objetos padrão
- Propriedades de valor
- Propriedades de função
- eval()
- isfinite()
- isNaN()
- parseFloat()
- parseInt()
- Objetos fundamentais
- Object
- Function
- Boolean
- Error
- EvalError
- InternalError
- RangeError
- ReferenceError
- StopIteration
- SyntaxError
- TypeError
- URIError
- Array
O JavaScript tem primitivos, objetos e funções. Todos todos eles são valores e são tratados como objetos, até mesmo os primitivos.
Objetos globais se referem aos objetos no escopo global (somente se o modo estrito/strict mode do ECMAScript 5 não for usado; Nesse caso retorna undefined). O objeto global pode ser acessado usando o operador this no escopo global. De fato, o escopo global consiste em propriedades do objeto global, incluindo propriedades herdadas, se houver.
Propriedades globais retornam um valor simples; eles não tem propriedades ou métodos.
A propriedade Infinity
é um valor numérico que representa infinito.
var maxNumber = Math.pow(10, 1000); // max positive number
if (maxNumber === Infinity) {
console.log("Let's call it Infinity!");
// expected output: "Let's call it Infinity!"
}
console.log(1 / maxNumber);
// expected output: 0
A propriedade NaN
é um valor representativo de Not-A-Number.
function sanitise(x) {
if (isNaN(x)) {
return NaN;
}
return x;
}
console.log(sanitise('1'));
// expected output: "1"
console.log(sanitise('NotANumber'));
// expe
A propriedade undefined
que representa o valor primitivo undefined.
function test(t) {
if (t === undefined) {
return 'Undefined value!';
}
return t;
}
var x;
console.log(test(x));
// expected output: "Undefined value!"
A propriedade null
representa a ausência intencional de qualquer valor de objeto.
function getVowels(str) {
var m = str.match(/[aeiou]/gi);
if (m === null) {
return 0;
}
return m.length;
}
console.log(getVowels('sky'));
// expected output: 0
Estas funções globais que são chamadas globalmente ao invés de em um objeto—retornam diretamente seus resultados a quem chama.
Se o argumento de eval()
não é uma string, eval()
retorna o argumento inalterado. No exemplo a seguir, o construtor String
é especificado, e eval()
retorna um objeto String em vez de avaliar a string.
eval(new String("2 + 2"));
// retorna um objeto String contendo "2 + 2"
eval("2 + 2");
// retorna 4
A função global isFinite()
determina se o valor transmitido é um número finito. Se necessário, o parâmetro é primeiro convertido a um número.
isFinite(Infinity);
// false
isFinite(NaN);
// false
isFinite(-Infinity);
// false
isFinite(0);
// true
isFinite(2e64);
// true
isFinite(null);
// true
isFinite("0");
// true, teria sido false com o
// mais robusto Number.isFinite("0")
A função isNAN()
determina se o valor é NaN
ou não.
isNaN(NaN);
// true
isNaN(undefined);
// true
isNaN({});
// true
isNaN(true);
// false
isNaN(null);
// false
isNaN(37);
// false
// strings
isNaN("37");
// false: "37" is converted to the number 37 which is not NaN
isNaN("37.37");
// false: "37.37" is converted to the number 37.37 which is not NaN
isNaN("");
// false: the empty string is converted to 0 which is not NaN
isNaN(" ");
// false: a string with spaces is converted to 0 which is not NaN
// dates
isNaN(new Date());
// false
isNaN(new Date().toString());
// true
// Esse é um falso positivo e é a razão para isNaN não seja totalmente confiável.
isNaN("blabla")
// true: "blabla" é convertido para número.
// A análise desse número falha e retorna NaN como resultado.
A função parseFloat()
analisa um argumento string e retorna um número de ponto flutuante.
//Os exemplo a seguir sempre retorna 3.14
parseFloat("3.14");
parseFloat("314e-2");
parseFloat("0.0314E+2");
parseFloat("3.14more non-digit characters");
A função parseInt()
analisa um argumento string e retorna um inteiro na base especificada.
//Os exemplo a seguir sempre retorna 15
parseInt(" 0xF", 16);
parseInt(" F", 16);
parseInt("17", 8);
parseInt(021, 8);
parseInt("015", 10);
parseInt(15.99, 10);
parseInt("FXX123", 16);
parseInt("1111", 2);
parseInt("15*3", 10);
parseInt("15e2", 10);
parseInt("15px", 10);
parseInt("12", 13);
A função decodeURI()
decodifica Uniform Resource Identifier (URI) previamente criado por encodeURI ou por uma rotina similar.
decodeURI(encodedURI)
O método decodeURIComponent()
decodifica um componente Identificador Uniforme de Recursos (URI) criado anteriormente por um encodeURIComponent ou por uma rotina semelhante.
decodeURIComponent(encodedURI)
A função encodeURI()
codifica um URI (Uniform Resource Identifier) substituindo cada instância de certos caracteres por uma, duas, três ou quatro seqüências de escape representando a codificação UTF-8 do caractere (serão apenas quatro seqüências de escape para caracteres compostos). de dois caracteres "substitutos").
encodeURI(URI)
O método encodeURIComponent()
codifica uma URI (Identificador recurso uniforme) substituindo cada ocorrência de determinados caracteres por um, dois, três, ou quatro seqüências de escape que representam a codificação UTF-8 do caractere (só será quatro seqüências de escape para caracteres compostos por dois caracteres "substituto").
encodeURIComponent(str);
Estes são objetos básicos e fundamentais nos quais todos os outros objetos são baseados. Isso inclui objetos que representam objetos genéricos, funções e erros.
Cria um novo objeto.
new Object( [ valor ] )
// qualquer valor
O construtor Function cria um novo objeto Function. Em JavaScript cada função é, na verdade, um objeto Function.
new Function ([arg1[, arg2[, ...argN]],] functionBody)
arg1, arg2, ... argN
Nomes para serem usandos pela função como nomes formais de argumentos. Cada um deve ser uma string que corresponde para uma válida identidade JavaScript ou uma lista de certas strings separadas com uma vírgula; por exemplo "x", "theValue". our "a,b".
functionBody
Uma string que contém as instruções JavaScript que compõem a definição da função.
- arguments
- arguments.@@iterator
- arguments.callee
- arguments.caller
- arguments.length
- Arrow functions
- AsyncFunction
- AsyncFunction.prototype
- Default parameters
- Function
- function.apply
- function.bind
- function.call
- Function.length
- Function.name
- Function.prototype
- function.toString
- Functions
- GeneratorFunction
- GeneratorFunction.prototype
- get
- Method definitions
- rest parameters
- set
O objeto Boolean é um objeto wrapper para um valor booleano.
new Boolean([value])
// value Opcional. O valor inicial do objeto Boolean.
- Boolean
- Boolean.prototype
- boolean.toString
- boolean.valueOf
O construtor de Error cria um objeto de erro. Instâncias de objetos Error são lançadas quando erros de tempo de execução ocorrem. O objeto Error também pode ser usado como objeto base para exceções definidas pelo usuário. Veja abaixo tipos de erro padrões embutidos.
new Error([message[, fileName[, lineNumber]]])
message
Descrição do erro legível.
fileName
O valor da propriedade fileName no objeto de erro criado. O padrão é o nome do arquivo contendo o código que chamou o construtor de Error().
lineNumber
O valor da propriedade lineNumber no objeto de Error criado. O padrão é o número da linha contendo a invocação do construtor Error()
.
- Error
- error.message
- error.name
- Error.prototype
- error.toString
- Errors
- Errors: Already has pragma
- Errors: Array sort argument
- Errors: Bad octal
- Errors: Bad radix
- Errors: Bad regexp flag
- Errors: Bad return or yield
- Errors: Called on incompatible type
- Errors: Cant access lexical declaration before init
- Errors: Cant define property object not extensible
- Errors: Cant delete
- Errors: Cant redefine property
- Errors: Cyclic object value
- Errors: Dead object
- Errors: Delete in strict mode
- Errors: Deprecated caller or arguments usage
- Errors: Deprecated expression closures
- Errors: Deprecated octal
- Errors: Deprecated source map pragma
- Errors: Deprecated String generics
- Errors: Deprecated toLocaleFormat
- Errors: Equal as assign
- Errors: For-each-in loops are deprecated
- Errors: Getter only
- Errors: Identifier after number
- Errors: Illegal character
- Errors: in operator no object
- Errors: Invalid array length
- Errors: Invalid assignment left-hand side
- Errors: Invalid const assignment
- Errors: Invalid date
- Errors: Invalid for-in initializer
- Errors: Invalid for-of initializer
- Errors: invalid right hand side instanceof operand
- Errors: JSON bad parse
- Errors: Malformed formal parameter
- Errors: Malformed URI
- Errors: Missing bracket after list
- Errors: Missing colon after property id
- Errors: Missing curly after function body
- Errors: Missing curly after property list
- Errors: Missing formal parameter
- Errors: Missing initializer in const
- Errors: Missing name after dot operator
- Errors: Missing parenthesis after argument list
- Errors: Missing parenthesis after condition
- Errors: Missing semicolon before statement
- Errors: More arguments needed
- Errors: Negative repetition count
- Errors: No non-null object
- Errors: No properties
- Errors: No variable name
- Errors: Non configurable array element
- Errors: Not a codepoint
- Errors: Not a constructor
- Errors: Not a function
- Errors: Not defined
- Errors: Precision range
- Errors: Property access denied
- Errors: Read-only
- Errors: Redeclared parameter
- Errors: Reserved identifier
- Errors: Resulting string too large
- Errors: Stmt after return
- Errors: Strict Non Simple Params
- Errors: Too much recursion
- Errors: Typed array invalid arguments
- Errors: Undeclared var
- Errors: Undefined prop
- Errors: Unexpected token
- Errors: Unexpected type
- Errors: Unnamed function statement
- Errors: Unterminated string literal
- Errors: Var hides argument
O Objeto EvalError indica um erro em relação a função global eval(). Esta exceção não é mais lançada pelo JavaScript, no entanto, o objeto EvalError ainda permanece compatível.
new EvalError([message[, fileName[, lineNumber]]])
message
Descrição do erro legível.
fileName
O nome do arquivo que contém o código que causa a exceção
lineNumber
O número da linha do código que causa a exceção
These are the base objects representing numbers, dates, and mathematical calculations.
These objects represent strings and support manipulating them.
These objects represent collections of data which are ordered by an index value. This includes (typed) arrays and array-like constructs.
Array é uma estrutura de dados que armazena uma coleção de elementos de tal forma que cada um dos elementos contidos nele possa ser acessado por meio de um índice(index) ou uma chave(key). Essa estrutura pode ser conhecida como variável indexada, coleções indexadas, coleções de dados, vetor(para arrays unidimensionais) e matriz (para arrays bidimensionais).
Mas no Javascript o array
é um objeto global que é usado na construção dessa estrutura de dado.
Reference Array
- array.@@iterator
- Array.@@species
- array.@@unscopables
- array.concat
- array.copyWithin
- array.entries
- array.every
- array.fill
- array.filter
- array.find
- array.findIndex
- array.flatMap
- array.flatten
- array.forEach
- Array.from
- array.includes
- array.indexOf
- Array.isArray
- array.join
- array.keys
- array.lastIndexOf
- Array.length
- array.map
- Array.of
- array.pop
- Array.prototype
- array.push
- array.reduce
- array.reduceRight
- array.reverse
- array.shift
- array.slice
- array.some
- array.sort
- array.splice
- array.toLocaleString
- array.toString
- array.unshift
- array.values
- ArrayBuffer.@@species
- arrayBuffer.byteLength
- ArrayBuffer.isView
- ArrayBuffer.prototype
- arrayBuffer.slice
- ArrayBuffer.transfer
Existe algumas forma de criar um array
:
// Array via construtor pode ser utilizado com ou sem new.
var array1 = new Array(elem0, elem1);
var array2 = Array(elem0, elem1);
// Array literal é definido com o número de elementos especificado
var array3 = ['elem0', 'elem1'];
Forma de acesar um item do array
atraves da sua índice(index):
var first = array1[0];
// elem0
var last = array1[array1.length - 1];
// array1
// tratando uma interação dentro de uma função:
function ArrayElements(element, index, array) {
console.log('a[' + index + '] = ' + element);
}
array1.forEach(ArrayElements);
// a[0] = elem0
// a[1] = elem1
// usando forEach direto no objeto array
array1.forEach(function(element, index, array) {
console.log('a[' + index + '] = ' + element);
});
// a[0] = elem0
// a[1] = elem1
Exemplo de algoritmo:
var fibonacci = []
fibonacci[1] = 1
fibonacci[2] = 1
for(var i = 3; i <= 20; i++) {
fibonacci[i] = fibonacci[i-1] + fibonacci[i-2];
}
for (var i = 1; i < fibonacci.length; i++) {
console.log(fibonacci[i])
}
Existe alguns métodos para inserir elementos num array.
Um deles é atribuindo valores aos seus elementos. Por exemplo:
var array3 = [];
array3[0] = 'elem0';
array3[1] = 'elem1';
array3[2] = 'elem2';
console.log(array3)
// ["elem0", "elem1", "elem2"]
Um deles é atribuindo valores aos seus elementos. Por exemplo:
var array3 = [];
array3[0] = 'elem0';
array3[1] = 'elem1';
array3[2] = 'elem2';
console.log(array3)
// ["elem0", "elem1", "elem2"]
These objects represent collections which use keys; these contain elements which are iterable in the order of insertion.
SIMD vector data types are objects where data is arranged into lanes.
These objects represent and interact with structured data buffers and data coded using JavaScript Object Notation (JSON).
Additions to the ECMAScript core for language-sensitive functionalities.
A block statement is used to group zero or more statements. The block is delimited by a pair of curly brackets.
Terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.
Terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.
An empty statement is used to provide no statement, although the JavaScript syntax would expect one.
Executes a statement if a specified condition is true. If the condition is false, another statement can be executed.
Evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case.
Throws a user-defined exception.
Marks a block of statements to try, and specifies a response, should an exception be thrown.
Declares a variable, optionally initializing it to a value.
Declares a block scope local variable, optionally initializing it to a value.
Declares a read-only named constant.
Declares a function with the specified parameters.
Generator Functions enable writing iterators more easily.
Declares an async function with the specified parameters.
Specifies the value to be returned by a function.
Declares a class.
- Classes
- constructor
- extends
- static
Creates a loop that executes a specified statement until the test condition evaluates to false. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once.
Creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement executed in the loop.
Iterates a specified variable over all values of object's properties. For each distinct property, a specified statement is executed.
Iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.
Iterates over iterable objects (including arrays, array-like objects, iterators and generators), invoking a custom iteration hook with statements to be executed for the value of each distinct property.
Creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement.
Invokes any available debugging functionality. If no debugging functionality is available, this statement has no effect.
Used to export functions to make them available for imports in external modules, another scripts.
Used to import functions exported from an external module, another script.
Provides a statement with an identifier that you can refer to using a break or continue statement.
Extends the scope chain for a statement.
Basic keywords and general expressions in JavaScript.
The this keyword refers to the function's execution context.
The function keyword defines a function expression.
The class keyword defines a class expression.
The function* keyword defines a generator function expression.
Pause and resume a generator function.
Delegate to another generator function or iterable object.
The async function defines an async function expression.
Pause and resume an async function and wait for the promise's resolution/rejection.
Array initializer/literal syntax.
Object initializer/literal syntax.
Regular expression literal syntax.
Grouping operator.
Left values are the destination of an assignment.
Member operators provide access to a property or method of an object (object.property and object["property"]).
The new operator creates an instance of a constructor.
In constructors, new.target refers to the constructor that was invoked by new.
The super keyword calls the parent constructor.
Spread syntax allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) are expected.
Postfix/prefix increment and postfix/prefix decrement operators.
Postfix increment operator.
Postfix decrement operator.
Prefix increment operator.
Prefix decrement operator.
A unary operation is operation with only one operand.
The delete operator deletes a property from an object.
The void operator discards an expression's return value.
The typeof operator determines the type of a given object.
The unary plus operator converts its operand to Number type.
The unary negation operator converts its operand to Number type and then negates it.
Bitwise NOT operator.
Logical NOT operator.
Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value.
Addition operator.
Subtraction operator.
Division operator.
Multiplication operator.
Remainder operator.
Exponentiation operator.
A comparison operator compares its operands and returns a Boolean value based on whether the comparison is true.
The in operator determines whether an object has a given property.
The instanceof operator determines whether an object is an instance of another object.
Less than operator.
Greater than operator.
Less than or equal operator.
Greater than or equal operator.
- Note: => is not an operator, but the notation for Arrow functions.
The result of evaluating an equality operator is always of type Boolean based on whether the comparison is true.
Equality operator.
Inequality operator.
Identity operator.
Nonidentity operator.
Operations to shift all bits of the operand.
Bitwise left shift operator.
Bitwise right shift operator.
Bitwise unsigned right shift operator.
Bitwise operators treat their operands as a set of 32 bits (zeros and ones) and return standard JavaScript numerical values.
Bitwise AND.
Bitwise OR.
Bitwise XOR.
Logical operators are typically used with boolean (logical) values, and when they are, they return a boolean value.
Logical AND.
Logical OR.
(condition ? ifTrue : ifFalse) The conditional operator returns one of two values based on the logical value of the condition.
An assignment operator assigns a value to its left operand based on the value of its right operand.
Assignment operator.
Multiplication assignment.
Division assignment.
Remainder assignment.
Addition assignment.
Subtraction assignment
Left shift assignment.
Right shift assignment.
Unsigned right shift assignment.
Bitwise AND assignment.
Bitwise XOR assignment.
Bitwise OR assignment.
{a, b} = {a:1, b:2} Destructuring assignment allows you to assign the properties of an array or object to variables using syntax that looks similar to array or object literals.
The comma operator allows multiple expressions to be evaluated in a single statement and returns the result of the last expression.
The function keyword can be used to define a legacy generator function inside an expression. To make the function a legacy generator, the function body should contains at least one yield expression.
The expression closure syntax is a shorthand for writing simple function.
Array comprehensions.
Generator comprehensions.
This chapter documents how to work with JavaScript functions to develop your applications.