let obj = { message : "A message" } obj.message //"A message" obj["message"] //"A message"
undefined
: obj.otherProperty //undefined
let french = {}; french["merci beaucoup"] = "thank you very much"; french["merci beaucoup"]; //"thank you very much"
toString()
method, if possible): et obj = {}; //Number obj[1] = "Number 1"; obj[1] === obj["1"]; //true //Object let number1 = { toString : function() { return "1"; } } obj[number1] === obj["1"]; //true
number1
object is used as the key. When attempting to access a property, it is converted to line 1
, and the result of this conversion is used as a key. let book = { title : "The Good Parts", author : { firstName : "Douglas", lastName : "Crockford" } } book.author.firstName; //"Douglas"
let app = {}; app.authorService = { getAuthors : function() {} }; app.bookService = { getBooks : function() {} };
this
.this
loses context. let obj = {}; obj.message = "This is a message"; // obj.otherMessage = "A new message"; // delete obj.otherMessage; //
__proto__
, pointing to a prototype object, from which the object inherits properties.Object.prototype
: var obj = {}; obj.__proto__ === Object.prototype; //true
{}
, is actually not so empty, since it contains a reference to an Object.prototype
. In order to create a truly empty object, you need to use the following construction: Object.create(null)
(1.23).toFixed(1); //"1.2" "text".toUpperCase(); //"TEXT" true.toString(); //"true"
Number
, String
, and Boolean
.Number.prototype
prototype, which is derived from the Object.prototype
: var no = 1; no.__proto__ === Number.prototype; //true no.__proto__.__proto__ === Object.prototype; //true
String.prototype
. The prototype of the boolean objects is the Boolean.prototype
. The prototype of the arrays (which are also objects) is Array.prototype
.Function.prototype
. Functions have methods like bind()
, apply()
and call()
.null
and undefined
values) inherit properties and methods from Object.prototype
. This leads to the fact that, for example, all of them have a toString()
method.Object.assign()
method. It allows you to add a new function to Object
if it is not available in it.Array.from()
, which, if there is no from()
method in the Array
object, equips it with this method.String.prototype.trim()
allows you to equip all string objects with a trim()
method: let text = " A text "; text.trim(); //"A text"
Array.prototype.find()
allows you to equip all arrays with the find()
method. The polyfill for Array.prototype.findIndex()
works in a similar way: let arr = ["A", "B", "C", "D", "E"]; arr.indexOf("C"); //2
Object.create()
command allows you to create new objects with a given prototype object. This command is used in JavaScript to implement the single inheritance mechanism. Consider an example : let bookPrototype = { getFullTitle : function(){ return this.title + " by " + this.author; } } let book = Object.create(bookPrototype); book.title = "JavaScript: The Good Parts"; book.author = "Douglas Crockford"; book.getFullTitle();//JavaScript: The Good Parts by Douglas Crockford
Object.assign()
command copies properties from one or more objects to the target object. It can be used to implement a multiple inheritance scheme. Here is an example : let authorDataService = { getAuthors : function() {} }; let bookDataService = { getBooks : function() {} }; let userDataService = { getUsers : function() {} }; let dataService = Object.assign({}, authorDataService, bookDataService, userDataService ); dataService.getAuthors(); dataService.getBooks(); dataService.getUsers();
Object.freeze()
command allows you to freeze an object. In such an object can not add new properties. Properties cannot be deleted, and their values ​​cannot be changed. Using this command, the object becomes immutable or immutable: "use strict"; let book = Object.freeze({ title : "Functional-Light JavaScript", author : "Kyle Simpson" }); book.title = "Other title";//: Cannot assign to read only property 'title'
Object.freeze()
command performs the so-called “shallow freezing” of objects. This means that objects nested in a “frozen” object can be modified. In order to carry out a “deep freezing” of an object, one must recursively “freeze” all its properties.Object.assign()
command: let book = Object.freeze({ title : "JavaScript Allongé", author : "Reginald Braithwaite" }); let clone = Object.assign({}, book);
let timer = { fn : null, start : function(callback) { this.fn = callback; }, stop : function() {}, }
timer.fn;//null timer.start = function() { console.log("New implementation"); }
Object.create()
and Object.freeze()
methods.timerPrototype
containing all the methods needed by different instances of the object. After that, create an object that is a successor of timerPrototype
: let timerPrototype = Object.freeze({ start : function() {}, stop : function() {} }); let timer = Object.create(timerPrototype); timer.__proto__ === timerPrototype; //true
start()
and stop()
methods cannot be redefined: "use strict"; timer.start = function() { console.log("New implementation"); } //: Cannot assign to read only property 'start' of object
Object.create(timerPrototype)
can be used to create multiple objects with the same prototype. function Timer(callback){ this.fn = callback; } Timer.prototype = { start : function() {}, stop : function() {} } function getTodos() {} let timer = new Timer(getTodos);
new
keyword. An object created using a constructor function named FunctionConstructor
will receive the prototype FunctionConstructor.prototype
: let timer = new Timer(); timer.__proto__ === Timer.prototype;
Timer.prototype = Object.freeze({ start : function() {}, stop : function() {} });
new Timer()
is executed, the same actions are performed as the following newTimer()
function newTimer()
: function newTimer(){ let newObj = Object.create(Timer.prototype); let returnObj = Timer.call(newObj, arguments); if(returnObj) return returnObj; return newObj; }
Timer.prototype
. Then the Timer
function is called, setting the fields for the new object.class
keyword and the corresponding constructs associated with it. Consider an example : class Timer{ constructor(callback){ this.fn = callback; } start() {} stop() {} } Object.freeze(Timer.prototype);
class
-based class keyword named ClassName
will have a prototype ClassName.prototype
. When creating an object based on a class, you need to use the keyword new
: let timer= new Timer(); timer.__proto__ === Timer.prototype;
Object.freeze(Timer.prototype);
Object.keys()
command returns an array containing all property keys of the object. It can be used to iterate through all the properties of an object: function logProperty(name){ console.log(name); // console.log(obj[name]); // } Object.keys(obj).forEach(logProperty);
_
): class Timer{ constructor(callback){ this._fn = callback; this._timerId = 0; } }
function TodoStore(callback){ let fn = callback; function start() {}, function stop() {} return Object.freeze({ start, stop }); }
fn
variable is private. Only start()
and stop()
methods are publicly available. These methods cannot be modified from the outside. This keyword is not used here, so when using this method of creating objects, the problem of losing the this
context is irrelevant.return
command uses an object literal containing only functions. Moreover, these functions are declared in closure, they share a common state. To “freeze” the public API of an object, the command Object.freeze()
already known to you is used.Timer
object. In this material you can find its full implementation.Object.create()
method, for the organization of multiple inheritance - Object.assign()
. You can use factory functions to create encapsulated objects.Source: https://habr.com/ru/post/420615/
All Articles