admin管理员组文章数量:1336631
What is o
?
If I am not mistaken, prototype means that F
is going to inherit from o
, but that is all I can understand here.
Can someone break this down for me:
if (!Object.create) {
Object.create = (function () {
var F = function(){};
return function (o) {
if (arguments.length !== 1) {
throw new Error('Object.create implementation only accepts one parameter.');
}
F.prototype = o;
return new F();
};
}());
What is o
?
If I am not mistaken, prototype means that F
is going to inherit from o
, but that is all I can understand here.
Can someone break this down for me:
if (!Object.create) {
Object.create = (function () {
var F = function(){};
return function (o) {
if (arguments.length !== 1) {
throw new Error('Object.create implementation only accepts one parameter.');
}
F.prototype = o;
return new F();
};
}());
Share
Improve this question
edited Nov 23, 2016 at 0:13
MrE
20.8k14 gold badges91 silver badges107 bronze badges
asked Jul 31, 2013 at 19:13
fish manfish man
1011 silver badge7 bronze badges
9
-
There seems to be an error here:
F
is initialized only once, so every time you callObject.create()
you change the prototype of every other object that you created with it. – Russell Zahniser Commented Jul 31, 2013 at 19:19 - @RussellZahniser I've been scratching my head over that too. I can't get that behavior to materialize when I try it however. – Pointy Commented Jul 31, 2013 at 19:22
- It appears that this is the remended polyfill from MDN, so I assume I am just missing something. – Russell Zahniser Commented Jul 31, 2013 at 19:24
-
@RussellZahniser the stuff that goes on with
new
and the prototype etc. is sufficiently bizarre that I almost never program that way :) – Pointy Commented Jul 31, 2013 at 19:26 -
@RussellZahniser, The fact that we are reusing the same empty function
F
as a constructor will not affect in any way the various created instances. Here's what happens:F
is a function that has an initialprototype
wich is an object that has it's prototype chain set toObject
and which has it's constructor property set toF
. When doingnew F()
, it creates a newobject
that has it's prototype chain set to the prototype ofF
and it runs the functionF
in the context of that new object. – plalx Commented Jul 31, 2013 at 19:30
2 Answers
Reset to default 4o
is a parameter that you pass into the function.
For example:
var myObj = Object.create({
myFn: function(){}
});
Then you can do:
myObj.myFn();
The context of that code can help you understand it better.
This polyfill covers the main use case which is creating a new object for which the prototype has been chosen but doesn't take the second argument into account. - https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#Polyfill
The code is intended to serve as a replacement for Object.create(proto [, propertiesObject ])
when Object.create
is not provided by the environment.
So, o
is just the object which will serve as the prototype of the new object you are creating.
In practice, this means that we can use o
as our superclass (or more correctly o
is the prototype of our superclass).
The example from the Mozilla docs makes this clear:
//subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Answering the ment, this feels a little too long for the ment box:
o
is the prototype of the object you want to inherit from. Technically it is an object too, everything in JS is an object. How is almost everything in Javascript an object?.
Why would you want to inherit from another object? What's the point of OOP?
What does this line do? F.prototype = o;
Lets repeat the full code, except with ments:
// if the execution environment doesn't have object.create, e.g. in old IE
if (!Object.create) {
// make object.create equal to something
Object.create = (function(){
// create a new object F which can be inherited from
function F(){}
// return a function which
return function(o){
// throw an error if
if (arguments.length != 1) {
throw new Error('Object.create implementation only accepts one parameter.');
}
// force F to inherit o's methods and attributes
F.prototype = o
// return an instance of F
return new F()
}
})() // this is an IIFE, so the code within is automatically executed and returned to Object.create
}
本文标签: new to javascript not sure what function (o) meansStack Overflow
版权声明:本文标题:new to javascript: not sure what function (o) means - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742405705a2468776.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论