illustrating javascript prototype & prototype chain
illustrating javascript prototype & prototype chain
图解 js 原型和原型链

proto & prototype
func;
// ƒ func (name) {
// this.name = name || `default name`;
// }
// 构造函数具有一个 prototype 属性,
func.prototype;
// {constructor: ƒ}
// 构造函数的 prototype 属性指向该构造函数的原型对象,
// 该构造函数的原型对象有一个 constructor 属性指向该构造函数本身,
func.prototype.constructor;
// ƒ func (name) {
// this.name = name || `default name`;
// }
func.prototype.constructor === func;
// true
// var funcInstance = new func();
funcInstance = new func();
// func {name: "default name"}
// 构造函数生成的实例对象具有一个 __proto__ 属性,
funcInstance.__proto__;
// {constructor: ƒ}
// 生实例对象的 __proto__ 属性指向其构造函数的原型对象,
funcInstance.__proto__ == func.prototype;
// true
// 生实例对象的没有 prototype 属性,
funcInstance.prototype;
// undefined

Function
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-07-18
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
*/
const log = console.log;
/************************************************/
Object;
// ƒ Object() { [native code] }
Object.prototype;
// {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
// constructor: ƒ Object()
// hasOwnProperty: ƒ hasOwnProperty()
// isPrototypeOf: ƒ isPrototypeOf()
// propertyIsEnumerable: ƒ propertyIsEnumerable()
// toLocaleString: ƒ toLocaleString()
// toString: ƒ toString()
// valueOf: ƒ valueOf()
// __defineGetter__: ƒ __defineGetter__()
// __defineSetter__: ƒ __defineSetter__()
// __lookupGetter__: ƒ __lookupGetter__()
// __lookupSetter__: ƒ __lookupSetter__()
// get __proto__: ƒ __proto__()
// set __proto__: ƒ __proto__()
Object.__proto__;
// ƒ () { [native code] }
Object.__proto__ === Object.prototype;
// false
/************************************************/
Function;
// ƒ Function() { [native code] }
Function.prototype;
// ƒ () { [native code] }
Function.__proto__;
// ƒ () { [native code] }
Function.__proto__ === Function.prototype;
// true
/************************************************/
Object.__proto__ === Function.__proto__;
// true
/************************************************/
function func (name) {
this.name = name || `default name`;
}
func;
// ƒ func (name) {
// this.name = name || `default name`;
// }
func.__proto__;
// ƒ () { [native code] }
func.__proto__.constructor;
// ƒ Function() { [native code] }
func.__proto__.constructor.prototype;
// ƒ () { [native code] }
func.__proto__.prototype;
// undefined
func.__proto__.__proto__;
// {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
// constructor: ƒ Object()
// hasOwnProperty: ƒ hasOwnProperty()
// isPrototypeOf: ƒ isPrototypeOf()
// propertyIsEnumerable: ƒ propertyIsEnumerable()
// toLocaleString: ƒ toLocaleString()
// toString: ƒ toString()
// valueOf: ƒ valueOf()
// __defineGetter__: ƒ __defineGetter__()
// __defineSetter__: ƒ __defineSetter__()
// __lookupGetter__: ƒ __lookupGetter__()
// __lookupSetter__: ƒ __lookupSetter__()
// get __proto__: ƒ __proto__()
// set __proto__: ƒ __proto__()
func.__proto__.__proto__.constructor;
// ƒ Object() { [native code] }
func.__proto__.__proto__.prototype;
// undefined
func.__proto__.__proto__.__proto__;
// null
/************************************************/
func.prototype;
// {constructor: ƒ}
// constructor: ƒ func(name)
// __proto__: Object
func.prototype.constructor;
// ƒ func (name) {
// this.name = name || `default name`;
// }
func.prototype.constructor.prototype;
// {constructor: ƒ}
// constructor: ƒ func(name)
// __proto__: Object
func.prototype.prototype;
// undefined
func.prototype.__proto__;
// {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
// constructor: ƒ Object()
// hasOwnProperty: ƒ hasOwnProperty()
// isPrototypeOf: ƒ isPrototypeOf()
// propertyIsEnumerable: ƒ propertyIsEnumerable()
// toLocaleString: ƒ toLocaleString()
// toString: ƒ toString()
// valueOf: ƒ valueOf()
// __defineGetter__: ƒ __defineGetter__()
// __defineSetter__: ƒ __defineSetter__()
// __lookupGetter__: ƒ __lookupGetter__()
// __lookupSetter__: ƒ __lookupSetter__()
// get __proto__: ƒ __proto__()
// set __proto__: ƒ __proto__()
func.prototype.__proto__.constructor;
// ƒ Object() { [native code] }
func.prototype.__proto__.prototype;
// undefined
func.prototype.__proto__.__proto__;
// null
/************************************************/
// compare
func.__proto__ === Function.prototype;
// true
func.__proto__ === Function.__proto__;
// true
func.__proto__ === Object.__proto__;
// true
func.prototype === Function.prototype;
// false
func.prototype === Object.prototype;
// false
func.prototype.constructor === func;
// true
Object
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-07-18
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
*/
const log = console.log;
/************************************************/
Object;
// ƒ Object() { [native code] }
Object.prototype;
// {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
// constructor: ƒ Object()
// hasOwnProperty: ƒ hasOwnProperty()
// isPrototypeOf: ƒ isPrototypeOf()
// propertyIsEnumerable: ƒ propertyIsEnumerable()
// toLocaleString: ƒ toLocaleString()
// toString: ƒ toString()
// valueOf: ƒ valueOf()
// __defineGetter__: ƒ __defineGetter__()
// __defineSetter__: ƒ __defineSetter__()
// __lookupGetter__: ƒ __lookupGetter__()
// __lookupSetter__: ƒ __lookupSetter__()
// get __proto__: ƒ __proto__()
// set __proto__: ƒ __proto__()
Object.__proto__;
// ƒ () { [native code] }
Object.__proto__ === Object.prototype;
// false
/************************************************/
Function;
// ƒ Function() { [native code] }
Function.prototype;
// ƒ () { [native code] }
Function.__proto__;
// ƒ () { [native code] }
Function.__proto__ === Function.prototype;
// true
/************************************************/
Object.__proto__ === Function.__proto__;
// true
/************************************************/
var obj = {
name: "xgqfrms",
};
obj;
// {name: "xgqfrms"}
// name: "xgqfrms"
// __proto__: Object
obj.__proto__;
// {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
// constructor: ƒ Object()
// hasOwnProperty: ƒ hasOwnProperty()
// isPrototypeOf: ƒ isPrototypeOf()
// propertyIsEnumerable: ƒ propertyIsEnumerable()
// toLocaleString: ƒ toLocaleString()
// toString: ƒ toString()
// valueOf: ƒ valueOf()
// __defineGetter__: ƒ __defineGetter__()
// __defineSetter__: ƒ __defineSetter__()
// __lookupGetter__: ƒ __lookupGetter__()
// __lookupSetter__: ƒ __lookupSetter__()
// get __proto__: ƒ __proto__()
// set __proto__: ƒ __proto__()
obj.__proto__.constructor;
// ƒ Object() { [native code] }
obj.__proto__.constructor.prototype;
// {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}
// constructor: ƒ Object()
// hasOwnProperty: ƒ hasOwnProperty()
// isPrototypeOf: ƒ isPrototypeOf()
// propertyIsEnumerable: ƒ propertyIsEnumerable()
// toLocaleString: ƒ toLocaleString()
// toString: ƒ toString()
// valueOf: ƒ valueOf()
// __defineGetter__: ƒ __defineGetter__()
// __defineSetter__: ƒ __defineSetter__()
// __lookupGetter__: ƒ __lookupGetter__()
// __lookupSetter__: ƒ __lookupSetter__()
// get __proto__: ƒ __proto__()
// set __proto__: ƒ __proto__()
obj.__proto__.prototype;
// undefined
obj.__proto__.__proto__;
// null
obj.__proto__.__proto__.constructor;
// Uncaught TypeError: Cannot read property 'constructor' of null
obj.__proto__.__proto__.prototype;
// Uncaught TypeError: Cannot read property 'prototype' of null
obj.__proto__.__proto__.__proto__;
// Uncaught TypeError: Cannot read property '__proto__' of null
/************************************************/
obj.prototype;
// undefined
obj.prototype.constructor;
// Uncaught TypeError: Cannot read property 'constructor' of undefined
obj.prototype.constructor.prototype;
// Uncaught TypeError: Cannot read property 'constructor' of undefined
obj.prototype.prototype;
// Uncaught TypeError: Cannot read property 'prototype' of undefined
obj.prototype.__proto__;
// Uncaught TypeError: Cannot read property '__proto__' of undefined
obj.prototype.__proto__.constructor;
// Uncaught TypeError: Cannot read property '__proto__' of undefined
obj.prototype.__proto__.prototype;
// Uncaught TypeError: Cannot read property '__proto__' of undefined
obj.prototype.__proto__.__proto__;
// Uncaught TypeError: Cannot read property '__proto__' of undefined
/************************************************/
// compare
obj.__proto__ === Function.prototype;
// false
obj.__proto__ === Function.__proto__;
// false
obj.__proto__ === Object.__proto__;
// false
obj.prototype === Function.prototype;
// false
obj.prototype === Object.prototype;
// false
obj.prototype.constructor === obj;
// Uncaught TypeError: Cannot read property 'constructor' of undefined
obj.__proto__.constructor.prototype === Object.prototype;
//true
refs

xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
illustrating javascript prototype & prototype chain的更多相关文章
- javascript 之 prototype 浅析
prototype 原型 javascript 是一种 prototype based programming 的语言, 而与我们通常的 class based programming 有很大 的区别 ...
- Javascript中prototype属性详解 (存)
Javascript中prototype属性详解 在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Javascript语言体系中,是不 ...
- (转载)详解Javascript中prototype属性(推荐)
在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Javascript语言体系中,是不存在类(Class)的概念的,javascript中不 ...
- Javascript: 从prototype漫谈到继承(2)
本文同时也发表在我另一篇独立博客 <Javascript: 从prototype漫谈到继承(2)>(管理员请注意!这两个都是我自己的原创博客!不要踢出首页!不是转载!已经误会三次了!) 上 ...
- JavaScript 笔记 ( Prototype )
这阵子实在好忙 ( 这样说好像也不是一两个月了... ),然后因为工作伙伴都是 JavaScript 神之等级的工程师,从中也学到不少知识,毕竟就是要和强者工作才会成长呀!为了想好好瞭解他们写的程式码 ...
- Javascript Array.prototype.some()
当我们使用数组时,查找数组中包含某个特殊的项是非常常见的动作.下面例子是一个简单的实现: 01 planets = [ 02 "mercury", 03 " ...
- JavaScript和prototype
Protoype这个词在javascript中可以有两种理解: 第一种是作为javascript中的一个属性,其一般出现的形式为:类名.prototype. prototype 属性让你有能力向对象添 ...
- 在 JavaScript 中 prototype 和 __proto__ 有什么区别
本文主要讲三个 问题 prototype 和 proto function 和 object new 到底发生了什么 prototype 和 proto 首先我们说下在 JS 中,常常让我们感到困惑的 ...
- javascript继承—prototype最优两种继承(空函数和循环拷贝)
一.利用空函数实现继承 参考了文章javascript继承-prototype属性介绍(2) 中叶小钗的评论,对这篇文章中的方案二利用一个空函数进行修改,可以解决创建子类对象时,父类实例化的过程中特权 ...
随机推荐
- 2.kafka架构深入——生产者
一个topic有多个partition,每个partition又有多个副本,在这些副本中又有一个leader和多个follower. 1)分区的原因 (1)方便在集群中扩展,每个Partition可以 ...
- all header field names in both HTTP requests and HTTP responses are case-insensitive.
https://tools.ietf.org/html/rfc6455#section-4.2.1 Please note that according to [RFC2616], all heade ...
- C++学习之STL(一)vector
前言 C++ Primer Plus读书笔记(三)复合类型 中已经简单介绍过vector是什么,这个系列主要是介绍STL特性. 声明 vector<ElemType> c; //创建一个空 ...
- 从一片森林(JavaScript)到另一片森林(C++)
从JavaScript到C Plus Plus 作为一个忠诚的Web开发者,JavaScript几乎是我这一年多以来的首选,不管是开发网站后端服务,还是开发跨端应用,我都会首选一个使用JavaScri ...
- Language Guide (proto3) | proto3 语言指南(七)更新消息类型
Updating A Message Type - 更新消息类型 如果现有的消息类型不再满足您的所有需要(例如,您希望消息格式有一个额外的字段),但是您仍然希望使用用旧格式创建的代码,不要担心!在不破 ...
- CentOS 安装TFTP
1.当然是使用yum安装最直接,一共会安装3个东东tftp.i386tftp-server.i386xinetd.i386[root@localhost CentOS]# yum -y install ...
- EXCEL序列
- Codeforces Round #626 (Div. 2) B. Count Subrectangles
题目连接:https://codeforces.com/contest/1323/problem/B 题意:给一个大小为n的a数组,一个大小为m的b数组,c数组是二维数组c[i][j]=a[i]*b[ ...
- 【NOIP 2015 D1 T2】信息传递(图论--带权并查集/bfs)
题目:有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学.游戏开始时,每人都只知道自己的生日.之后每一轮中, ...
- OkHttp Client Ignore certificate
import okhttp3.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.net.ssl.*; i ...