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

JavaScript的原型与原型链 #1

Closed
GGXXMM opened this issue Aug 4, 2019 · 1 comment
Closed

JavaScript的原型与原型链 #1

GGXXMM opened this issue Aug 4, 2019 · 1 comment
Labels
⭐️ js js knowledge

Comments

@GGXXMM
Copy link
Owner

GGXXMM commented Aug 4, 2019

原型(prototype)

概念

每个函数创建的时候,其内部会初始化一个prototype属性,prototype属性指向一个对象,这个对象正是调用其构造函数创建的实例的原型。

function Person(){}
console.log(typeof Person.prototype)// object

image

原型链(__proto__)

概念

当访问一个对象的属性时,如果自己内部没有找到该属性,就会向上访问原型对象,一直检索到Object内置对象,从而形成一条由__proto__连接的原型链。

function Person(){}
Person.prototype.name = 'zhangsan';
Person.prototype.age = 22;
Person.prototype.say = function(){
     console.log(this.name+','+this.age);
}
var p1 = new Person();
var p2 = new Person();
p2.name = "lisi";
p2.say();// lisi,22
p1.say();// zhangsan,22

image

@Guanrui1
Copy link

可以留个联系方式吗?

@GGXXMM GGXXMM added the ⭐️ js js knowledge label Dec 7, 2021
@GGXXMM GGXXMM closed this as completed Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⭐️ js js knowledge
Projects
None yet
Development

No branches or pull requests

2 participants