一,instanceof:

instanceof检测左侧的__proto__原型链上,是否存在右侧的prototype原型. 我在之前的两篇文章

[js高手之路]构造函数的基本特性与优缺点

[js高手之路]一步步图解javascript的原型(prototype)对象,原型链

已经分享过了.

         function CreateObj(uName) {
this.userName = uName;
this.showUserName = function () {
return '100';
}
}
CreateObj.prototype.showUserName = function () {
return this.userName;
}
var obj1 = new CreateObj('ghostwu');
var obj2 = new CreateObj('卫庄'); console.log( obj1 instanceof CreateObj ); //true
console.log( obj2 instanceof CreateObj ); //true
console.log( obj1 instanceof Object ); //true
console.log( obj2 instanceof Object ); //true

二、isPrototypeOf:

如果隐式原型__proto__指向调用isPrototypeOf()方法的对象原型( CreateObj ), 那么这个方法就返回true,如:

         var obj1 = new CreateObj('ghostwu');
var obj2 = new CreateObj('卫庄');
console.log( CreateObj.prototype.isPrototypeOf( obj1 ) ); //true
console.log( CreateObj.prototype.isPrototypeOf( obj2 ) ); //true

因为obj1,obj2的隐式原型__proto__指向的都是CreateObj.prototype, 有朋友可能会问CreateObj.prototype上面根本就没有isPrototypeOf这个方法,怎么可以

调用呢?

是的,没错,但是CreateObj.prototype的隐式原型__proto__指向了Object.prototype, 而isPrototypeOf存在Object.prototype上,所以就能够调用

三、Object.getPrototypeOf

获取实例的隐式原型(__proto__)的指向,因为obj1,obj2的__proto__都指向CreateObj.prototype

         var obj1 = new CreateObj('ghostwu');
var obj2 = new CreateObj('卫庄');
console.log( Object.getPrototypeOf( obj1 ) === CreateObj.prototype ); //true
console.log( Object.getPrototypeOf( obj2 ) === CreateObj.prototype ); //true

四,实例访问属性和方法时,遵循就近查找原则

实例先在自己身上查找,有,就停止查找,如果没有,就沿着实例的__proto__继续往上查找,有,就停止查找,如果没有就继续沿着原型链一直往上查找,如果

所有的原型对象上都没有,那就是undefined.

         function CreateObj(uName) {
this.userName = uName;
}
CreateObj.prototype.showUserName = function () {
return this.userName;
}
CreateObj.prototype.age = 22; var obj1 = new CreateObj('ghostwu');
obj1.age = 20;
var obj2 = new CreateObj('卫庄'); console.log( obj1.age ); //20--->来自实例
console.log( obj2.age ); //22--->来自原型对象 delete obj1.age;
console.log( obj1.age ); //22--->来自原型

五,hasOwnProperty

判断属性是实例上的还是原型对象上的,如果是实例上的,返回true, 原型上的返回false

         function CreateObj(uName) {
this.userName = uName;
}
CreateObj.prototype.showUserName = function () {
return this.userName;
}
CreateObj.prototype.age = 22;
var obj1 = new CreateObj('ghostwu');
obj1.age = 20;
var obj2 = new CreateObj('卫庄');
console.log( obj1.age ); //20--->来自实例
console.log( obj1.hasOwnProperty( 'age' ) ); //true
console.log( obj2.age ); //22--->来自原型对象
console.log( obj2.hasOwnProperty( 'age' ) ); //false
delete obj1.age;
console.log( obj1.age ); //22--->来自原型
console.log( obj1.hasOwnProperty( 'age' ) ); //false

六、in操作符

判断属性是否在实例或者原型对象上,只要一个满足条件,返回值都是true

         function CreateObj(uName) {
this.userName = uName;
}
CreateObj.prototype.showUserName = function () {
return this.userName;
}
CreateObj.prototype.age = 22;
var obj1 = new CreateObj('ghostwu');
obj1.age = 20;
console.log( 'age' in obj1 ); //true
var obj2 = new CreateObj('卫庄');
console.log( 'age' in obj2 ); //true
delete obj1.age;
console.log( 'age' in obj1 ); //true
console.log( 'user' in obj1 ); //false
console.log( 'user' in obj2 ); //false

七,结合in和hasOwnProperty的用法,可以封装一个函数判断这个属性是否在原型对象上, 返回值为true:在原型对象上, false:不在原型对象上

         function CreateObj(uName) {
this.userName = uName;
}
CreateObj.prototype.showUserName = function () {
return this.userName;
}
CreateObj.prototype.age = 20;
function hasPrototypeProperty( obj, name ){
return !obj.hasOwnProperty( name ) && ( name in obj );
}
var obj1 = new CreateObj('ghostwu');
var obj2 = new CreateObj('卫庄');
obj1.age = 10;
console.log( hasPrototypeProperty( obj1, 'age' ) ); //false
console.log( hasPrototypeProperty( obj2, 'age' ) ); //true

八、for...in 可以枚举实例和原型对象上的属性和方法,前提是:该属性和方法是可以枚举

          function CreateObj(uName) {
this.userName = uName;
}
CreateObj.prototype.showUserName = function () {
return this.userName;
}
CreateObj.prototype.age = 20;
var obj = new CreateObj( 'ghostwu' ); for( var key in obj ){
console.log( key ); //userName,age,showUserName
}
console.log( Object.prototype );
for( var key in Object.prototype ){
console.log( key );//枚举不了, Object.prototype上的属性和方法默认不可枚举,枚举属性为false
}

[js高手之路]原型对象(prototype)与原型链相关属性与方法详解的更多相关文章

  1. [js高手之路] dom常用API【appendChild,insertBefore,removeChild,replaceChild,cloneNode】详解与应用

    本文主要讲解DOM常用的CURD操作,appendChild(往后追加节点),insertBefore(往前追加节点),removeChild(移除节点),replaceChild(替换节点),clo ...

  2. JavaScript的原型对象prototype、原型属性__proto__、原型链和constructor

    先画上一个关系图: 1. 什么是prototype.__proto__.constructor? var arr = new Array; 1. __proto__是原型属性,对象特有的属性,是对象指 ...

  3. 原型对象prototype和原型属性[[Prototype]]

    构造器:可以被 new 运算符调用, Boolean,Number,String,Date,RegExp,Error,Function,Array,Object 都是构造器,他们有各自的实现方式. 比 ...

  4. [js高手之路] 设计模式系列课程 - jQuery的链式调用与灵活的构造函数

    一.我们从一个简单的构造函数+原型程序开始 var G = function(){}; G.prototype = { length : 5, size : function(){ return th ...

  5. js面向对象之公有、私有、静态属性和方法详解

    现下,javascript大行其道,对于网站开发人员来说,javascript是必需掌据的一门语言,但随着jquery等框架的流行和使用,许多人对于原生javascript缺乏深入的理解,习惯了函数式 ...

  6. [js高手之路] html5 canvas系列教程 - 认识canvas以及基本使用方法

    canvas是html5中引入的一个新元素,俗称画布,既然是画布,当然是用来画图的.canvas技术指的是利用javascript操作canvas元素绘制图形的技术,要使用canvas,一定要浏览器支 ...

  7. JavaScript原生对象属性和方法详解——Array对象

    http://www.feeldesignstudio.com/2013/09/native-javascript-object-properties-and-methods-array/ lengt ...

  8. python学习笔记8--面向对象--属性和方法详解

    属性: 公有属性  (属于类,每个类一份) 普通属性  (属于对象,每个对象一份) 私有属性    (属于对象,跟普通属性相似,只是不能通过对象直接访问) 方法:(按作用) 构造方法 析构函数 方法: ...

  9. [js高手之路] 设计模式系列课程 - jQuery的extend插件机制

    这里在之前的文章[js高手之路] 设计模式系列课程 - jQuery的链式调用与灵活的构造函数基础上增加一个extend浅拷贝,可以为对象方便的扩展属性和方法, jquery的插件扩展机制,大致就是这 ...

随机推荐

  1. 如何安装Orchard

    本篇文章主要讲解如何安装Orchard,首先说一下Orchard的安装方式有如下几种: 通过Microsoft WebMatrix(Microsoft Web Platform Installer)安 ...

  2. Out of mind - 魔术纸

    魔术纸 显示屏与纸张的完美结合.类似电子墨水.柔性显示器.魔术纸柔软似真正的纸张.用魔术纸做成的电子书,控制器在书轴处. 每一页能显示不同的东西.一本书可以完全按页显示在电子书上.可以换一本书来显示. ...

  3. 各种demo:css实现三角形,css大小梯形,svg使用

    各种demo: 1.css实现正方形 思路:width为0:height为0:使用boder-width为正方形的边长的一半,不占任何字节:border-style为固体:border-color为正 ...

  4. (转)Spring Boot Junit单元测试

    场景:在项目开发中要测试springboot工程中一个几个dao和service的功能是否正常,初期是在web工程中进行要素的录入,工作量太大.使用该单元测试大大减小了工作强度. Junit这种老技术 ...

  5. 【Mysql】MySQL与Oracle的大小写问题

    转载来源:http://aofengblog.blog.163.com/blog/static/63170212010101065030136/ MySQL与Oracle在大小写处理上的区别: 1MY ...

  6. C#小爬虫,通过URL进行模拟发送接收数据

    public async Task<string> SendDataAsync(HttpMethod httpMethod, string requestUrl, HttpContent ...

  7. Android 性能测试——Heap Viewer 工具

    Android 性能测试--Heap Viewer 工具 Heap Viewer能做什么? 实时查看App分配的内存大小和空闲内存大小 发现Memory Leaks Heap Viewer使用条件 5 ...

  8. Android学习笔记-TextView(文本框)(二)

    2.4 使用autoLink属性识别链接类型 当文字中出现了URL,E-Mail,电话号码,地图的时候,我们可以通过设置autoLink属性:当我们点击 文字中对应部分的文字,即可跳转至某默认APP, ...

  9. bash中(),{},(()),[],[[]]的区别

    前言:在bash中遇到各种括号,同时在进行字符数值比较判定时,总是不断出现问题,于是通过参考<advanced bash-scripting guide>,同时在centos 6.7版本上 ...

  10. Java基础之集合框架类及泛型简介

    Collection接口 Collection 通用的常见方法 add()添加一个元素,可以指定脚标 addAll()将一个collection放入 clear()清除 remove()删除元素,返回 ...