Reputation: 4546
I know you can set the prototype of a new object with this function (read mozzilla docu) but does it also create own properties if it's used within an object literal like this
return Object.create(this);
I also know this method from a Klass literal wich only copies the instance methods
var subclass = function() { };
subclass.prototype = parent.prototype;
klass.prototype = new subclass;
mostly I am interested in the object.create method
EDIT
var Klass = {
init: function(){},
prototype: {
init: function(){}
},
create: function(){
var object = Object.create(this);
console.log('object with class create');
console.log(object);
console.log("object's parent is this");
console.log(this);
object.parent = this;
object.init.apply(object, arguments);
console.log('returned object from create');
console.log(object);
return object;
},
inst: function(){
var instance = Object.create(this.prototype);
console.log('de instance na object create');
console.log(instance);
instance.parent = this;
instance.init.apply(instance, arguments);
console.log('arguments in inst');
console.log(arguments);
return instance;
},
proxy: function(func){
var thisObject = this;
return(function(){
return func.apply(thisObject, arguments);
});
},
include: function(obj){
var included = obj.included || obj.setup;
for(var i in obj)
this.fn[i] = obj[i];
if (included) included(this);
},
extend: function(obj){
var extended = obj.extended || obj.setup;
for(var i in obj)
this[i] = obj[i];
if (extended) extended(this);
}
};
Klass.fn = Klass.prototype;
Klass.fn.proxy = Klass.proxy;
thanks, Richard
Upvotes: 0
Views: 105
Reputation: 664297
does it also create own properties
If you read the docs, it says No - unless you tell it to do so with a second argument. It's basic use is to create a new, empty object with its internal prototype set to the argument. The second argument would work like defineProperties
then.
if it's used within an object literal like this
return Object.create(this);
I don't see any object literal here, but as you don't use the second parameter the returned object will have no own properties.
Upvotes: 1
Reputation: 8611
Summary
Creates a new object with the specified prototype object and properties.
So lets take a look at an simple example with an Object instantiated with the new
keyword and one with Object.create
;
function objectDotCreate() {
this.property = "is defined";
this.createMe = function () {
return Object.create(this);
};
}
var myTestObject = new objectDotCreate();
console.log(myTestObject, myTestObject.createMe());
Now taking a look at the console output
Left: new
Right: Object.create
As you can see both create a new Instance of an Object, with their properties.
Only Object.create
Creates a new object with the specified prototype object and properties.
And new
(MDN)
[...] creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function.
So the Instance, created using Object.create
, gains access to the properties,because they are shadowed by its prototype
and the one where new
was used, has its own properties, defined by its constructor.
So no it doesn't create its own Properties. (Though you can pass an Object to directly define the Objects property descriptors)
Upvotes: 2