javascript - A little doubt about js prototype and _proto_
伊谢尔伦
伊谢尔伦 2017-05-19 10:26:34
0
9
801


The function will create the prorotype attribute to point to the prototype object, so prototype is a function-specific attribute and all objects in chrome seem to have the _proro_ attribute, so what is _proto_ ? Pointer to prototype object? What about _proto_ in the prototype object? Please help me solve my doubts. Thank you

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(9)
Peter_Zhu

_proto_ is an attribute on the instance object. Let me show it to you according to the content written in the book:

    function Person(){
    
    }
    Person.prototype = {
        constructor:Person,
        name : "Nicholas",
        age  : 29,
        sayName(){
            alert(this.name);
        }
    }
    var son = new Person();

There is a _proto_ pointer on the son instance. This pointer points to its prototype object, not the constructor. Therefore, there is a prototype chain search process. All instances can find the method attributes on the prototype through the prototype chain and share them. . The top of this prototype chain is Object. When searching for properties and f methods on an instance, the search order is as follows: instance ==> constructor ==> prototype object ==> Object.

Regarding whether the end is an Object, there is a special case. The newly added Object.create() method in ES5 can change it. Ignore this factor for the time being. In most cases, the end is an Object, so the instance can use toString( on Object. ), valueOf(), hasOwnProperty() and other methods.

It’s not a good explanation, I hope it helps! This part can be figured out by reading the content in the book, combining the code in the book and practicing. It is recommended to refer to all the contents in Chapter 6 of "Height 3 Edition".

给我你的怀抱

Please refer to Chapter 6 of "Advanced Programming with JavaScript"

刘奇

__proto__ is an attribute of an object, through which you can access the attributes of the prototype object. The process is like this, when we use the new operator to instantiate an object, such as

function People(name) {
    this.name = name;
    this.speak = function(){
        alert(this.name);
    };
}
People.prototype.run = function(){
    alert('速度挺快');
};
var person = new People('jack');
当调用person.speak()时,这个时候系统扫描了一下person 本身的属性和方法,发现自己有就调用了。
如果调用的是person.run(),显然这个对象是没有这个方法的,
这个方法是在原型对象的,所以在他要去到原型中找。
当前对象是怎么和原型对象联系起来的呢
就是通过__proto__
既然所有的对象都有__proto__属性
原型对象也是对象同样也有__proto__属性
,这样就形成了原型链,也就是继承。
一级一级的查找你调用的方法和属性,找到后立即返回。
这个点有点难理解,可以和你共同探讨。
给我你的怀抱

__proto__ is an attribute of an object,
prototype is an attribute of a function,
functions are also objects, and they also have __proto__

曾经蜡笔没有小新

I feel like there is no answer to the above point, __proto__ is a line, the destination of this line is the Prototype object (prototype object), __proto__ is used to connect the object and its prototype

仅有的幸福

First of all, correct the error in the question's description: prototype is not a unique attribute of the function.
If you really want to understand this issue, please study the 原型链related knowledge of js.
As the friend above said, please refer to Chapter 6 of "Advanced Programming with JavaScript". After you have a basic understanding, it will be clearer to look at this picture.

给我你的怀抱

You can probably think of it this way, it’s easier to understand, but it may not be deep enough, but this is the best way to understand it. You can think of the methods and properties of the function that you don’t want to expose to you as the data stored in the Prototype object. The function is like the race of a race. It's very powerful and has potential power, just like a Saiyaman. There are many things that you can't see directly. You can only force him out through certain methods. Okay, then __proto__ can do it and use him. You will understand. It turns out that I can destroy the earth. Hahahaha

巴扎黑

@ Linshuizhaohua
As shown in the picture, does this mean that Object and Function are both top-level objects?

滿天的星座

Thank you for your answers. After looking at Elevation 3, the function will create a prototype attribute pointing to the prototype object. prototype is indeed a unique attribute of the function. There will be a pointer to the prototype object [[prototype]] inside the instance. In Firefox Chrome Safari, every All objects support the _proto_ attribute, which should be the browser implementation of [[prototype]].

Each constructor has a prototype object. The prototype object contains a pointer to the constructor. The instance object contains an internal pointer to the prototype object. The relationship between the prototype and the instance forms a prototype chain. Inheritance mainly relies on the prototype chain to make the prototype object equal to an instance of another type. The prototype object will contain a pointer to another prototype, and the other prototype will contain a pointer to the constructor layer by layer. The default prototype of all functions is object. The instance will point to obj.prototype, and finally I understand why all values ​​can use Object.prototype.toString.call(value) to determine the type.

Constructor <-------- Prototype object < -------- Instance pointer

"It seems that it makes sense to read more, read more newspapers, eat less snacks and sleep more"
"Do you have a girlfriend?"
" new GirlFriend"
"..."

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template