Coding patterns for initializing objects - constructors (new) and Object.create () (Crockford)

Note. This is not a question of inheriting the classic and prototype. It's about which encoding patterns to use to initialize objects. Class constructors create and initialize objects, while avoiding the new operator and switching to Object.create() creates only an object and sets up a prototype chain. I have not yet found an online resource that explains the best coding examples for creating and initializing using the Crockford Object.create() approach.

If I have a constructor (in my head this is done by my class, although I know that classes do not exist at all in standard JavaScript)

 function Person(first, last) { this.name = { first: first, last: last }; } Person.prototype.tellName = function() { return this.name.first + ' ' + this.name.last; } 

Then I can create an instance of this type

 var p1 = new Person('John', 'Doe'); var p2 = new Person('Sven', 'Svensson'); 

And change Person.name.first and Person.name.last separately

 p1.tellName(); // Output: 'John Doe' p2.tellName(); // Output: 'Sven Svensson' p1.name.first = 'Peter'; p2.name.last = 'Celery'; 

And execute the function of the Person.tellName() object with the following output

 p1.tellName(); // Output: 'Peter Doe' p2.tellName(); // Output: 'Sven Celery' 

This is very similar to how I would like to build such a class in C ++ or Java.


What patterns do I use to create or initiate an object, where can I assign a nested object using the Crockford ( Object.create() -ish) approach instead of new ?

eg.

 ... // some code that does the same stuff as defining a class + constructor ... var p1 = ??????? var p2 = ??????? // The following is the code and behavior I'm looking to get p1.tellName(); // Output: 'John Doe' p2.tellName(); // Output: 'Sven Svensson' p1.name.first = 'Peter'; p2.name.last = 'Celery'; p1.tellName(); // Output: 'Peter Doe' p2.tellName(); // Output: 'Sven Celery' 
+5
source share
1 answer

Instead of:

 function Person(first, last) { this.name = { first: first, last: last }; } Person.prototype.tellName = function() { return this.name.first + ' ' + this.name.last; } 

You just have:

 function Person(first, last) { return { name: { first: first, last: last }, tellName: function() { return this.name.first + ' ' + this.name.last; } }; }; 

Or, if you prefer what person.create() looks like, then:

 var person = { create: function(first, last) { return { name: { first: first, last: last }, tellName: function() { return this.name.first + ' ' + this.name.last; } }; } }; 

But in the second case, you will have an unnecessary object ( person ) containing only one function ( person.create() ).

You do not need Object.create and new , since this is for inheritance, which you said that you do not care about. This will allow you to do:

 var p1 = Person('John', 'Doe'); var p2 = Person('Sven', 'Svensson'); 

An interesting fact is that you can still use new before person.create if you want, but that will not bring any effect. If you need to use an existing function, you can explicitly set the this context using .call

 // with your original `Person` var p1 = Person.call({}, 'John', 'Doe'); var p2 = Person.call({}, 'Sven', 'Svensson'); 

This would not set a prototype, since the function is not called as a constructor. See this answer about what the prototypical answer does and does not do - in a line it exchanges functionality not about building properties of your objects.

+3
source

Source: https://habr.com/ru/post/1210722/


All Articles