JS的prototype
初步理解:
|
1
2
3
4
|
function name(obj){ alert(obj)//"uw3c"}name("uw3c") |
|
1
2
3
4
5
|
function name(obj){ alert(obj)//"uw3c"}var test = new name("uw3c")test(); |
|
1
2
3
4
5
|
function uw3c(){}var test = new uw3c();alert(typeof uw3c);//functionalert(typeof test);//object |
|
1
2
3
4
5
6
|
function name(){ alert(JSON.stringify(name.prototype))//{},是个空对象}name();var test = new name();alert(JSON.stringify(test.prototype))//undefined,不存在这个对象 |
什么是prototype:
|
1
2
3
4
5
|
var function{ prototype:prototype{ constructor:constructor == function }} |

prototype的作用:
|
1
2
3
4
5
|
function uw3c(){}uw3c.prototype.name = "a";var test = new uw3c();alert(test.name)//"a"; |
|
1
2
3
4
5
6
7
|
var name = "js";function uw3c(name){ alert(this.name);//"css"}uw3c.prototype.name = "css";var test = new uw3c();test(); |
|
1
2
|
var test={};uw3c.call(test); |
第二步将该对象(test)内置的原型对象设置为构造函数(就是uw3c)prototype 属性引用的那个原型对象。
第三步就是将该对象(test)作为this 参数调用构造函数(就是uw3c),完成成员设置等初始化工作。
其中第二步中出现了一个新名词就是内置的原型对象,注意这个新名词跟prototype对象不是一回事, 为了区别我叫它inobj,inobj就指向了函数uw3c的prototype对象。在uw3c的prototype对象中出现的任何属性或者函数都可以在test对象中直接使用,这个就是JS中的原型继承了。
prototype是继承还是克隆:
|
1
2
3
4
5
6
|
function uw3c(){}uw3c.prototype.name = "b";var test = new uw3c();alert(JSON.stringify(test));//{}alert(test.name);//"b" |
prototype的优点:
|
1
2
3
4
5
6
7
|
function uw3c(name){ alert("姓名:" + name + ",年龄:" + this.age + ",性别:" + this.sex);}uw3c.prototype.age = 15;uw3c.prototype.sex = "man";var test1 = new uw3c("css");//姓名:css,年龄:15,性别:manvar test2 = new uw3c("js");//姓名:js,年龄:15,性别:man |
JS的prototype的更多相关文章
- 深入理解js的prototype以及prototype的一些应用
上一篇讲了js的prototype概念,在这里回顾一下prototype的定义: prototype是函数的一个属性,并且是函数的原型对象.引用它的必然是函数,这个应该记住. 但是,很奇怪,各位看官, ...
- 简单理解js的prototype属性
在进入正文之前,我得先说说我认识js的prototype这个东西的曲折过程. 百度js的prototype的文章,先看看,W3School关于prototype的介绍: 你觉得这概念适合定义js的pr ...
- Bom和Dom编程以及js中prototype的详解
一.Bom编程: 1.事件练习: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
- JS的prototype和__proto__(含es6的class)
JS的prototype和__proto__(含es6的class) 一.prototype和__proto__的概念 prototype是函数的一个属性(每个函数都有一个prototype属性),这 ...
- JS的prototype和__proto__、constructor
看了JS的prototype和__proto__这篇文章,才感觉很清晰了,对于原型这块,以前经常把这些属性弄不清楚, 明白了之后保存下整理下: prototype: 是函数的一个属性(每个函数都有一个 ...
- js的prototype的详解(1)
一.什么是JavaScript中对象的prototype属性 JavaScript中对象的prototype属性,是用来返回对象类型原型的引用的.我们使用prototype属性提供对象的类的一组基本功 ...
- JS 测试 Prototype
JS 测试 Prototype 测试 JavaScript 框架库 - Prototype 引用 Prototype 如需测试 JavaScript 库,您需要在网页中引用它. 为了引用某个库,请使用 ...
- 复习一下js的prototype 属性
<html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</t ...
- 如何使用 js 扩展 prototype 方法
如何使用 js 扩展 prototype 方法 expand prototype function enhancedLog(msg = ``) { // this.msg = msg; enhance ...
- js & object & prototype & __proto__ & prototype chain
js & object & prototype & proto & prototype chain constructor prototype === instance ...
随机推荐
- sax/dom/jdom/dom4j的区别
sax/dom/jdom/dom4j的区别[转] 博客分类: xml 网上能够查到很多的4种解析方式的区别,我再做一下摘录和总结,顺带给自己做个备份. SAX sax分析器在对xml文档进行分析时 ...
- 实时监听输入框值变化:oninput & onpropertychange
结合 HTML5 标准事件 oninput 和 IE 专属事件 onpropertychange 事件来监听输入框值变化. oninput 是 HTML5 的标准事件,对于检测 textarea, i ...
- xcode-git笔记
git initgit add .vi .gitignore /*将代码区蓝色字体的内容*/git commit -m "初次建立"git remote add origin ht ...
- JAVA 常用框架和工具
集成开发工具(IDE):Eclipse.MyEclipse.Spring Tool Suite(STS).Intellij IDEA.NetBeans.JBuilder.JCreator JAVA服务 ...
- leetcode-【hard】4. Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- centos7使用传统网卡名
http://serverfault.com/questions/692897/centos-7-disable-predictable-network-interface-names-during- ...
- C#有关数组内存的释放及动态数组问题
一.数组内存释放问题 数组内存的释放可以按照如下语句实现: string [] aa=new string[2]; aa[0]="A"; aa[1]="B"; ...
- teeChart 修改注册表实现无试用到期提示
teechart在注册表的跟目录为HKEY_CLASSES_ROOT\CLSID,以下的子目录会根据版本的不同发生改变,但是最底级的目录是TypeLib(例如[HKEY_CLASSES_ROOT\CL ...
- mapreduce出现类似死锁情况
在往hbase表里通过bulkload导数据时,某个mapreduce跑了一个多小时还没跑,看yarn界面,发现map还有一小部分没跑完,没跑完的map全在pending,running中没有,同时r ...
- VUE 入门基础(7)
八,事件处理器 监听事件 可以用v-on 指令监听DOM 事件来触发一些javaScript <div id="example-1"> <button v-on: ...