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的更多相关文章

  1. javascript 之 prototype 浅析

    prototype 原型 javascript 是一种 prototype based programming 的语言, 而与我们通常的 class based programming 有很大 的区别 ...

  2. Javascript中prototype属性详解 (存)

    Javascript中prototype属性详解   在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Javascript语言体系中,是不 ...

  3. (转载)详解Javascript中prototype属性(推荐)

    在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Javascript语言体系中,是不存在类(Class)的概念的,javascript中不 ...

  4. Javascript: 从prototype漫谈到继承(2)

    本文同时也发表在我另一篇独立博客 <Javascript: 从prototype漫谈到继承(2)>(管理员请注意!这两个都是我自己的原创博客!不要踢出首页!不是转载!已经误会三次了!) 上 ...

  5. JavaScript 笔记 ( Prototype )

    这阵子实在好忙 ( 这样说好像也不是一两个月了... ),然后因为工作伙伴都是 JavaScript 神之等级的工程师,从中也学到不少知识,毕竟就是要和强者工作才会成长呀!为了想好好瞭解他们写的程式码 ...

  6. Javascript Array.prototype.some()

    当我们使用数组时,查找数组中包含某个特殊的项是非常常见的动作.下面例子是一个简单的实现: 01 planets = [ 02     "mercury", 03     " ...

  7. JavaScript和prototype

    Protoype这个词在javascript中可以有两种理解: 第一种是作为javascript中的一个属性,其一般出现的形式为:类名.prototype. prototype 属性让你有能力向对象添 ...

  8. 在 JavaScript 中 prototype 和 __proto__ 有什么区别

    本文主要讲三个 问题 prototype 和 proto function 和 object new 到底发生了什么 prototype 和 proto 首先我们说下在 JS 中,常常让我们感到困惑的 ...

  9. javascript继承—prototype最优两种继承(空函数和循环拷贝)

    一.利用空函数实现继承 参考了文章javascript继承-prototype属性介绍(2) 中叶小钗的评论,对这篇文章中的方案二利用一个空函数进行修改,可以解决创建子类对象时,父类实例化的过程中特权 ...

随机推荐

  1. 如何使用 Vuepress

    项目结构 ├─docs │ ├─.vuepress --vuepress相关文件路径 (主要配置) │ │ ├─dist --build 打包生成路径 (自定义) │ │ ├─nav --导航条配置 ...

  2. Map类型数据导出Excel--poi

    https://blog.csdn.net/KevinChen2019/article/details/101064790 <dependency> <groupId>org. ...

  3. 3D运动类申明与实现

    #ifndef PKM3D_H #define PKM3D_H #include"kinematics.h" #include"Inventor/Qt/viewers/S ...

  4. 武装你的WEBAPI-OData常见问题

    本文属于OData系列 目录 武装你的WEBAPI-OData入门 武装你的WEBAPI-OData便捷查询 武装你的WEBAPI-OData分页查询 武装你的WEBAPI-OData资源更新Delt ...

  5. 分布式理论 PACELC 了解么?

    PACELC 基于 CAP 理论演进而来. CAP 理论是一个分布式系统中老生常谈的理论了: C(Consistency):一致性,所有节点在同一时间的数据完全一致. A(Availability): ...

  6. 设计模式(二)——Java简单工厂模式

    简单工厂模式 案例: 披萨的项目(要便于披萨种类的扩展,要便于维护) 1)披萨的种类很多(比如 GreekPizz.CheesePizz 等) 2)披萨的制作有 prepare,bake, cut, ...

  7. STM32通过rosserial接入ROS通讯开发

    作者:良知犹存 转载授权以及围观:欢迎添加微信公众号:羽林君 前言 主题:串口是一种设备间常用的通讯接口,rosserial将串口字符数据转发到标准ROS网络,并输出到rosout和其日志文件.本文将 ...

  8. AC自动机——看似KMP在跑,其实fail在跳

    先存代码 AC自动机(简单版) #include<bits/stdc++.h> #define maxn 1000007 using namespace std; int n,ans; i ...

  9. AtCoder Beginner Contest 163

    比赛链接:https://atcoder.jp/contests/abc163/tasks A - Circle Pond 题意 由半径输出圆周长. 代码 #include <bits/stdc ...

  10. 【bzoj 3232】圈地游戏(算法效率--01分数规划+图论--最小割)

    题目:DZY家的后院有一块地,由N行M列的方格组成,格子内种的菜有一定的价值,并且每一条单位长度的格线有一定的费用.DZY喜欢在地里散步.他总是从任意一个格点出发,沿着格线行走直到回到出发点,且在行走 ...