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.