prototype/constructor/__proto__之prototype
1任何对象都有__proto__属性 属性值Object
2并不是所有对象都有prototype属性。只有方法对象(构造函数)以及基本数据类型还有Array,有prototype属性;并且所有方法(对象)的prototype属性都是object
在网上有很多关于原型的讲解。在这里我用console.log()的方式给大家呈现。
//一、基本数据类型的原型
//console.log(String) String() 这时我们可以把它理解为构造函数
//console.log(typeof String) function
//console.log(String.prototype) String {}
//console.log(typeof String.prototype) object 说明String的prototype属性是一个object对象,既然是对象我们就可以给他添加属性或者方法。
String.prototype.cf=function(n){ //给String.prototype这个obj添加方法cf,字符串乘法
return new Array(n+1).join(this)
}
var str="cmf"
//console.log(str.cf(10)) cmfcmfcmfcmfcmfcmfcmfcmfcmfcmf
//console.log(String.prototype) String { cf=function()} String的原型是一个对象(obj)它有cf属性,这个属性对应着一个方法。
//二、构造函数的原型
function Info(m,n){ //构造函数
this.m=m
this.n=n
this.mn=m+"|"+n
}
//var 什么东西前面可以加var 基本数据类型(String Number true underfind) 或者 obj(null function Array)
//var info=new Info('cmf','man')
//console.log(typeof info) object 通过构造函数实例化出来的都是obj
//console.log(info) // info { m="cmf", n="man", mn="cmf|man"}
//console.log(typeof Info) //function 前面说过方法对象(构造函数)有prototype属性
//console.log(Info.prototype) // Info {}
//console.log(typeof Info.prototype) //object Info.prototype属性是一个对象,是对象我们就给可以给他定义方法或者属性。
//给构造函数原型(obj)定义方法(属性)
Info.prototype.name=function(){
return this.m+'name'
}
Info.prototype.age='18'
var info2=new Info('cmf','man') //实例化
/*
age:"18"在原型上
m:"cmf"
console.log(info2) mn:"cmf|man"
n:"man"
name:function()在原型上
*/
//console.log(info.name())
//三 为什么只有 方法对象以及基本数据类型有prototype属性,它们两有什么相似的地方
//第一个问题我不知道
//第二个问题
var str1=new String('adq')
var str2='xhb'
var str3='adq'
var arry=new Array(1)
var arry2=new Array(2)
/*
console.log(typeof str1) //object
console.log(typeof str2) //string
console.log(str1.cf(8)) //adqadqadqadqadqadqadqadq
console.log(str2.cf(8)) //xhbxhbxhbxhbxhbxhbxhbxhb
console.log(str1==str3) //true
console.log(str1===str3) //false
怎么理解呢 我们把所有的东西都理解为obj。基本数据类型理解为一个特殊的obj。
把上面的代码自己console.log一遍应该有体会了。
prototype/constructor/__proto__之prototype的更多相关文章
- prototype/constructor/__proto__之prototype简单应用
一.简单使用构造原型加prototype造简单的轮子. 1.想jQ那样获取HTML元素,先看JS代码 function Cmf() { //创建构造函数 this.arry = [] } Cmf.pr ...
- 原型模式Prototype,constructor,__proto__详解
最近由于在找工作,又拿起<JavaScript高级程序设计>看了起来,从中也发现了自己确实还是有很多地方不懂,刚刚看到原型模式这里,今天终于搞懂了,当然,我也不知道自己的理解是否有错. 1 ...
- prototype/constructor/__proto__之constructor。
1.constructor的字面意思就是构造.它是对象的一个属性,它对应的值是该对象的“构造者” //一.构造函数实例化的对象的constructor function Cmf(n,m){ this. ...
- prototype constructor __proto__
constructor, prototype, __proto__ 详解
- Js中Prototype、__proto__、Constructor、Object、Function关系介绍
一. Prototype.__proto__与Object.Function关系介绍 Function.Object:都是Js自带的函数对象.prototype,每一个函数对象都有一个显式的proto ...
- 15条规则解析JavaScript对象布局(__proto__、prototype、constructor)
大家都说JavaScript的属性多,记不过来,各种结构复杂不易了解.确实JS是一门入门快提高难的语言,但是也有其他办法可以辅助记忆.下面就来讨论一下JS的一大难点-对象布局,究竟设计JS这门语言的人 ...
- javascript--15条规则解析JavaScript对象布局(__proto__、prototype、constructor)
大家都说JavaScript的属性多,记不过来,各种结构复杂不易了解.确实JS是一门入门快提高难的语言,但是也有其他办法可以辅助记忆.下面就来讨论一下JS的一大难点-对象布局,究竟设计JS这门语言的人 ...
- 【转】Js中Prototype、__proto__、Constructor、Object、Function关系介绍
一 Prototype.__proto__与Object.Function关系介绍 Function.Object:Js自带的函数对象. prototype,每一个 ...
- 一篇彻底理解JS中的prototype、__proto__与constructor
1.基本类型不是对象(boolean.undefined.number.string) 2.引用类型都是对象(Array,function ,Object) 3.对象是通过函数创建,并且强调,对象字面 ...
随机推荐
- 使用 环境变量 来配置批量配置apache
使用apache的DAV假设了一个GIT仓库.需要为每个项目写一个配置项, 配置内容如下 <directory "/srv/abcdfdfdjkdfjkgjjdhjklfdjjfdfd ...
- Java虚拟机基础知识
写在前面 之前老大让做一些外包面试,我的问题很简单: 介绍一下工作中解决过比较有意思的问题. HashMap使用中需要注意的点. 第一个问题主要是想了解一下对方项目经验的含金量,第二个问题则是测试下是 ...
- 钥匙计数之一 - HDU 1438(状态压缩打表)
分析:首先想到每个钥匙的结尾有4种状态,不过题目还需要判断有三种不同的钥匙深度,所以每种深度结尾后有2^4种状态,0000->1111,不过题目还需需要有相邻的钥匙深度大于等于3,所以需要两种不 ...
- list去除重复
1. [代码][Python]代码 简单去重 ? 1 2 3 4 5 l = [1,2,3,3] l = list(set(l)) >>>l >>>[ ...
- 编译LOADCEPC.EXE程序
1.安装编译工具 安装MSVC152路径C:/MSVC; 安装MASM611可以自己指定E:/MASM611; 命令行编译 相关文件配置 修改setupen2.bat 如下: :PATH_DONE s ...
- 【转】Android通过Wifi来调试你的应用
http://stormzhang.com/android/2014/08/27/adb-over-wifi/ 在Android中调试我们经常要用一根USB数据线连接到手机和电脑,一方面麻烦不说,手机 ...
- hibernate官方新手教程 (转载)
hibernate官方新手教程第一部分 - 第一个Hibernate程序 首先我们将创建一个简单的控制台(console-based)Hibernate程序.我们使用内置数据库(in-memory d ...
- PHP运行出现Notice : Use of undefined constant 的解决方法【已测】
关闭 PHP 提示的方法 搜索php.ini:error_reporting = E_ALL改为:error_reporting = E_ALL & ~E_NOTICE还有个不是办法的办法就是 ...
- [Javascript] Modifying an Immutable.js Map()
We will now look at five methods that modify an Immutable.Map(). set update delete clear merge //set ...
- Redhat 官方Performance_Tuning_Guide
https://access.redhat.com/documentation/zh-CN/Red_Hat_Enterprise_Linux/6/html/Performance_Tuning_Gui ...