Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于Array.prototype.slice.call和Object.assign的思考 #1

Open
hxkuc opened this issue Apr 17, 2018 · 1 comment
Open

关于Array.prototype.slice.call和Object.assign的思考 #1

hxkuc opened this issue Apr 17, 2018 · 1 comment

Comments

@hxkuc
Copy link
Owner

hxkuc commented Apr 17, 2018

很简单的一个问题却发现一直没有认真总结
为什么Array调用slice需要使用原型,而Object不需要使用原型,首先直接在浏览器中调处控制台然后分别打印Array和Object
image
都是构造函数,然后我们分别做如下打印
image
从上面可以看出Object的assign是定义在构造函数里面的,而Array的slice是定义在其原型链上的
也就是两者的实现方式不一样,assign的定义应该是这样的

function Object () {}
Object.assign = function () {}

而slice的实现方式应该是下面这样

function Array () {}
Array.prototype.slice = function () {}

从下面这张图可以看到Object其他的几个函数也都是按照这种方式来创建的
image
在这里猜想大概是为了不让继承吧,所以这样操作的

@hxkuc
Copy link
Owner Author

hxkuc commented Jun 2, 2018

这是为了确保生成的数组纯净干净,确保属性和方法都在原型链上,数组只保存队列数据,暂时这么理解??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant