js深入学习-js prototype constructor属性区别
来源:互联网 发布:重庆正大软件职业学院 编辑:程序博客网 时间:2024/06/07 04:56
在很多js 插件中出现这两个属性的频率很高,我自己写插件时,也用到过,知道用,不知道具体的区别,今天研究了下,
constructor 返回的是对象(类型的实例)的构造函数,通过prototype 添加的属性和方法不会返回。
prototype 返回的是类型的原型,不会饭后构造函数部分。
实例如下:
<html><head><script type="text/javascript"> var cat=function (name,sex){ this.name=name; this.sex=sex; this.print=function(){ console.log("name:"+name+" \r\n sex:"+sex); } } cat.prototype.color="red"; cat.prototype.printColor=function(){ console.log("color:"+this.color+"\r\n name"+this.name); } var myCat=new cat("cavent","boy");</script ></head><body><h1 id="object">js prototype属性和constructor属性</h1></body></html>
在谷歌浏览器中的控制台,输入myCat.constructor ,显示如下
function (name,sex){ this.name=name; this.sex=sex; this.print=function(){ console.log("name:"+name+" \r\n sex:"+sex); } }输入cat.prototype 显示如下:
Object {color: "red", printColor: function}color: "red"constructor: function (name,sex){printColor: function (){arguments: nullcaller: nulllength: 0name: ""prototype: Object__proto__: function Empty() {}<function scope>__proto__: Object
截图如下:
0 0
- js深入学习-js prototype constructor属性区别
- js中的constructor 和prototype深入分析
- js中的constructor 和prototype深入分析
- js中的constructor 和prototype深入分析
- 深入分析js中的constructor 和prototype
- 深入分析js中的constructor 和prototype
- 深入分析js中的constructor 和prototype
- 深入分析js中的constructor 和prototype
- 深入分析js中的constructor 和prototype
- 深入分析js中的constructor 和prototype
- 深入分析js中的prototype,_proto_,constructor
- js constructor和prototype
- js中判断数据类型的属性typeof、instanceof、 constructor、 prototype
- js中的prototype与constructor
- js中的prototype和constructor
- JS Prototype constructor 转载之一
- js中的constructor 和prototype
- js 中的this,constructor ,prototype
- Django学习小记[2] —— Model
- 啊打撒的撒往往
- 安卓开发黄金搭档:android-studio+Genymotion模拟器
- 功夫的办法打不过
- 用cloud9+openshift+django开发web应用
- js深入学习-js prototype constructor属性区别
- dcbcdcxfg
- Windows下如何使用QT编写dll程序
- 阻赚转状作左
- 自庄族卒仔灼
- PackageManager
- js深入学习-回调函数之代码复用
- Fedora 19 Kdump 自动化测试套件 结题报告
- c primer plus第七章14题答案(新手请多指教)