好好爱自己!

再说javascript 的__proto__ 和prototype 属性

过了一段时间,没写 原生的 javascript 的了,感觉天天在用框架写代码,框架写代码完全限定死了你所需要思考的东西,只是在处理一些业务逻辑,真正的代码

都感觉不会写了。 突然发现,框架用的不熟悉,原生的代码也忘得差不多了。感觉很难受,人生不能这样子度过!

  重新翻开《javascript 高级程序设计》, 回归到本原。工作上用框架写代码没错,业余时间的话就要自己多写一点原生的代码,或者说研究、模仿、直到自己设计一个

框架出来。

js 中的类, 对象, 类的静态变量,类的继承 。
function Scope(){

这样就是定义了一个类了。

-------------------------------------------------------------------

This figure again shows that every object has a prototype. Constructor function Foo also has its own __proto__ which is Function.prototype, and which in turn also references via its __proto__property again to the Object.prototype. Thus, repeat, Foo.prototype is just an explicit property of Foo which refers to the prototype of b and c objects.

var b = new Foo(20);
var c = new Foo(30);

What are the differences between __proto__ and prototype properties?

 

Prototype VS. __proto__ VS. [[Prototype]]

When creating a function, a property object called prototype is being created automatically (you didn't create it yourself) and is being attached to the function object (the constructor).
Note: This new prototype object also points to, or has an internal-private link to, the native JavaScript Object.

Example:

function Foo () {
    this.name = 'John Doe';
}

// Foo has an object property called prototype.
// prototype was created automatically when we declared the function Foo.
Foo.hasOwnProperty('prototype'); // true

// Now, we can assign properties to to the prototype object without declaring it first.
Foo.prototype.myName = function () {
    return 'My name is ' + this.name;
}

If you will create a new object out of Foo using the new keyword, you basically creating (among other things) a new object that has an internal or private link to the function's prototype Foo we discussed earlier:

var b = new Foo();

b.[[Prototype]] === Foo.prototype  // true

 


The private linkage to that function's object called [[Prototype]]. Many browsers are providing us with a public linkage instead that called __proto__!

 

To be more specific, __proto__ is actually a getter function that belong to the native JavaScript Object and returns the internal-private prototype linkage of whatever the this binding is (returns the [[Prototype]] of b):

b.__proto__ === Foo.prototype // true

It is worth noting that starting of ECMAScript5, you can also use the getPrototypeOf method to get the internal private linkage:

Object.getPrototypeOf(b) === b.__proto__ // true

 


NOTE: this answer doesn't intend to cover the whole process of creating new objects or new constructors, but to help better understand what is __proto__prototype and [[Prototype]] and how it works.
posted @   立志做一个好的程序员  阅读(309)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示