关于prototype
之前听过课,可是这一块没怎么听懂,最近练了两个例子,又问了问小石同学,朦朦胧胧,感觉还是不太懂,记录点心得
最基本的例子
function Box(name,age){
this.name=name;
this.age=age;
this.run=function(){
return this.age+this.name;
}
}
var box=new Box('lee',);
console.log(box.run());
这个是传了两个参数,如果不传参的话,当然也行
function Box(){
this.name='age';
this.age='';
this.run=function(){
return this.age+this.name;
}
}
var box=new Box();
console.log(box.run());
如果用prototype的话,应该这样写
function Box(){
this.name='age';
this.age='';
}
Box.prototype.run=function(){
return this.age+this.name;
}
var box=new Box();
console.log(box.run());
也可以写成这样
function Box(){
this.name='age';
this.age='';
}
Box.prototype={
run:function(){
return this.age+this.name;}
}
var box=new Box();
console.log(box.run());
然后解决了我一个问题,以前我是这样写的,报错了
function box1(){
var sum=;
this.box2();
}
box1.prototype.box2=function(){
var count=this.sum+;
console.log(count);
} new box1()
其实,应该写成这样,不能用var,用var就变成了私有变量了
function box1(){
this.sum=;
this.box2();
}
box1.prototype.box2=function(){
var count=this.sum+;
console.log(count);
} new box1()
还有就是new出来是一个对象,必须用对象.XXXX才行
function box1(){
this.sum=;
}
box1.prototype.box2=function(){
var count=this.sum+;
return count;
}
console.log(new box1().box2())
如果是实例化方法和属性的话,他们的引用地址是不一样的
function box(name,age){
this.name=name; //实例化属性
this.age=age;
this.run=function(){ //实例化方法
return this.name+this.age+'运行中';
}
}
var box1=new box("lee",100); //实例化后地址为1
var box2=new box("lee",100); //实例化后地址为2
alert(box1.name==box2.name) //true
alert(box1.run()==box2.run()); //true
alert(box1.run==box2.run); //false 比较的是引用地址,方法的引用地址
如果是原型的属性和方法的话,他们的引用地址是一样的
function box(name,age){
this.name=name;
this.age=age;
}
box.prototype.run=function(){ //原型方法
return this.name+this.age+'运行中';
}
var box1=new box("lee",100); //原型方法,他们的地址是共享的
var box2=new box("lee",100); //原型方法,他们的地址是共享的 alert(box1.run==box2.run); //true 比较的是引用地址
根据上图,创建的每一个函数都有prototype属性,__proto__是一个指针,指向prototype原型对象,new出来的对象都有这个属性,他的作用是指向构造函数的原型属性constructor,通过这两个属性,就可以访问到原型里的属性和方法了。
function box(name,age){
this.name=name;
this.age=age;
}
box.prototype.run=function(){
return this.name+this.age+'运行中';
}
var box1=new box("lee",100);
box.prototype //box {}
box.prototype.constructor //function box(name,age){....} 指向本身
box1.constructor //function box(name,age){....} 指向本身
box1.constructor==box.prototype.constructor //true
box1.__proto__ //box {}
box1.__proto__==box.prototype //true
三、prototype字面量方法
function box(name,age){
this.name=name;
this.age=age;
}
box.prototype={
constructor:box, //强制指回box,如果不重新定义此属性,contructor指向了object,认为{}定义了一个数组
run:function(){
return this.name+this.age+'运行中';
}
}
var box1=new box("lee",100);
alert(box1.constructor)
@
关于prototype的更多相关文章
- js闭包 和 prototype
function test(){ var p=200; function q(){ return p++; } return q; } var s = test(); alert(s()); aler ...
- PHP设计模式(六)原型模式(Prototype For PHP)
原型设计模式: 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 原型设计模式简单的来说,顾名思义, 不去创建新的对象进而保留原型的一种设计模式. 缺点:原型设计模式是的最主要的缺点就 ...
- Function.prototype.toString 的使用技巧
Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...
- 分析js中的constructor 和prototype
在javascript的使用过程中,constructor 和prototype这两个概念是相当重要的,深入的理解这两个概念对理解js的一些核心概念非常的重要. 我们在定义函数的时候,函数定义的时候函 ...
- C#设计模式:原型模式(Prototype)及深拷贝、浅拷贝
原型模式(Prototype) 定义: 原型模式:用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象.被复制的实例被称为原型,这个原型是可定制的. Prototype Pattern也是一 ...
- 关于JS的prototype
在接触JS的过程中,随着理解的深入会逐渐的理解一些比较深奥的理论或者知识,那么今天我们来介绍一下比较难理解的prototype和constructor. 初步理解: 在说prototype和const ...
- js中的原型prototype
var arr1 = new Array(12,34,98,43,38,79,56,1); arr1.sum=function (){ var result = 0; for(var i=0; i&l ...
- [基础] Array.prototype.indexOf()查询方式
背景 最近在看Redux源码,createStore用于注册一个全局store,其内部维护一个Listeren数组,存放state变化时所有的响应函数. 其中store.subscribe(liste ...
- prototype,__proto__,constructor
proto属性: 所有对象都有此属性.但它不是规范里定义的属性,并不是所有JavaScript运行环境都支持.它指向对象的原型,也就是你说的继承链里的原型.通过Object.getPrototypeO ...
- js构造函数的方法与原型prototype
把方法写在构造函数内的情况我们简称为函数内方法,把方法写在prototype属性上的情况我们简称为prototype上的方法 函数内的方法: 使用函数内的方法我们可以访问到函数内部的私有变量,如果我们 ...
随机推荐
- LR网页细分图中的时间详解
Web Page Diagnostics: 1)DNS Resolution:浏览器访问一个网站的时候,一般用的是域名,需要dns服务器把这个域名解析为IP,这个过程就是域名解析时间,如果我们在局域网 ...
- Bigtable: A Distributed Storage System for Structured Data
https://static.googleusercontent.com/media/research.google.com/en//archive/bigtable-osdi06.pdf Abstr ...
- Apache Kafka源码分析 - ReplicaStateMachine
startup 在onControllerFailover中被调用, /** * Invoked on successful controller election. First registers ...
- 【Demo】微信上墙
先看看微信墙效果图 使用简单说明 关于微信公众号 回复 "上墙",点击授权文章进行授权 回复"#上墙内容" 即可发表上墙消息了 查看微信墙列表,点击这里 原文地 ...
- nrf51822-主从通信分析2
解决第三个问题:如何使能从机上的特征值的 notify功能,使其能通过notify方式发送数据 使能从机的notify功能是通过写0x0001到从机的那个具有notify功能的特征值的CCCD描述 ...
- angularJS自定义属性作为条件中转
<html> <head> <meta charset="utf-8"/> <title></title> </h ...
- Linux下对各种压缩文件处理
Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通常都是以.tar结尾的.生成tar包后,就可以用其它的程序来进 行压缩了,所以首先就来讲讲ta ...
- SQL集合函数中利用case when then 技巧
我们都知道SQL中适用case when then来转化数据库中的信息 比如 select (case sex when 0 then '男' else '女' end) AS sex from ...
- 浅谈负载均衡SLB、CLB和综合应用
SLB 服务器负载均衡(Server Load Balancing),可以看作HSRP(热备份路由器协议)的扩展,实现多个服务器之间的负载均衡. 虚拟服务器代表的是多个真实服务器的群集 ...
- nginx的内存管理
先来看内存池的实现,nginx的内存池实现的非常简单. 这里内存池的一些图表可以看老朱同学的slides : http://blog.zhuzhaoyuan.com/2009/09/nginx-int ...