This is my study book of JavaScript Defiitive Guide 6th Edition
In JS, classes are based on JS’s prototype-based inheritance mechanism. If two objects inherit properties from the same prototype object, then we say that they are instances of the same class. One of the most important features of JS classes is that the are dynamically extendable.
A class in JS is a set of objects that inherit properties from the same prototype object.
We can create classes without a constructor
The prototype property of the constructor is used as the prototype of the new object. This means that all objects created with the same constructor inherit from the same object and are therefore members of the same class.
function Person(name) {
this.name = name;
}
Person.prototype = {
isMe: function () {
return this.name == 'Deividy';
}
}
Person.prototype.toString = function() {
return "Noo!";
}
p = new Person('John');
console.log(p instanceof Person); // => true
console.log(p.constructor === Person); // => false
console.log(p.constructor.prototype === Person.prototype) // => false
console.log(p.prototype === Person) // => false
console.log(p.prototype === Person.prototype) // => false
console.log(p.__proto__ === Person) // => ralse
var F = function() {};
F.prototype.constructor === F; // => true
Person.prototype > Constructor = Person()
Constructor Object As we’ve noted, the constructor function (an object) defines a name for a JS class. Properties you add to this constructor object serve as class fields and class methods.
Prototype Object The properties of this object are inherited by all instances of the class, and properties whose values are functions behave like instance methods of the class.
Instance Object Each instance of a class is an object in its own right, and properties defined directly on an instance are not shared by any other instances. Nonfunction properties defined on instances behave as the instance fields of the class.
First, write a constructor function that sets instance properties on new objects. Second, define instance methods on the prototype object of the constructor. Third, define class fields and class properties on the constructor itself.
read moreThis is my study book of JavaScript Definitive Guide 6th Edition.
Literals of primitive type, like strings and numbers, evalute (obviously) to the same value each time they are encountered in a program. Object literals (or initializers) such as { } and [ ] create a new object each time they are encountered. If you write var a = [] in the body of a loop, for example, each iteration of the loop will create a new empty array.
Regular expression literals are a special case. The ECMAScript 3 specification says that a RegExp literal is converted to a RegExp object when the code is parsed, and each evaluation of the code returns the same object. The ECMAScript 5 specification reverses this and requires that each evaluation of a RegExp return a new object. IE has always implemented the ECMAScript 5 behavior and most current browsers have now switched to it, even before they fully implemented the standard.
^ $ . * + ? = ! : | \ / ( ) [ ] { }
read moreThis is my study book of CoffeeScript Accelerated JavaScript Development.
Just put your JS code surrounding it with backticks
console.log `impatient ? useBackticks() : learnCoffeeScript()`
we can execute a function using do
do fn
fn()
Again; - Every function creates a scope, and the only way to create a scope is to define a function - A variable lives in the outermost scope in which an assignment has been made to that variable - Outside of its scope, a variable is invisible
Variable’s scope is defined explicitly in JS using the var keyword while CS infers scope from assignments.
read moreThis is my study book of JavaScript Definitive Guide 6th Edition.
JS functions definitions can be nested within other functions, and they have access to any variable that are in scope where they are defined. This means that JS functions are closures. (I got the power!)
Functions can be defined:
var fn = function (args) { };
var fn = function fun(args) { };
var fn = (function() { }());
function fn () { }
Any legal JavaScript identifier can be a function name. Try to choose function names that are descriptive but concise. Striking the right balance is an art that comes with experience. Well-chosen function names can make a big difference in the readability (and thus maintainability) of your code.
Expression are evaluated to produce a value, but statements are executed to make omething happen
This is my study book of JavaScript Definitive Guide 6th Edition.
JS arrays are a specialized form of JS Object, and array indexes are really little more than property names that happen to be integers.
Inherit properties from Array.prototype
zero-base 32-bit indexes
Max index 2(32) - 2 (4294967294)
var a = [1, 2, 3, , ];
a.length; // => 4
var a = new Array(5);
a; // => [ , , , , ]
a[0]; // => undefined
var a = new Array(5, 4, 3, 2, 1);
a; // => [5, 4, 3, 2, 1]
Sparse array A sparse array is one in which the elements do not have contiguous indexes starting at 0.
Array that are dense, not sparse.
When an array is sparse, the length property is greater than the number of elements, and all we can say about it is that length is guaranteed to be larger than the index of every element in the array. A array will never have an element whose index is greather or equal to its length.
var a = [1, 2, 3];
delete a[1];
1 in a; // => false
a.length; // => 3
Deleting an array element is similar to (but subtly different than) assigning undefined to that element. Note that using delete on an array element does not alter the length property and does not shift elements with higher indexes down to fill in the gap that is left by the deleted property. If you delete an element from an array, the array becomes sparse.
read more