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的更多相关文章

  1. prototype/constructor/__proto__之prototype简单应用

    一.简单使用构造原型加prototype造简单的轮子. 1.想jQ那样获取HTML元素,先看JS代码 function Cmf() { //创建构造函数 this.arry = [] } Cmf.pr ...

  2. 原型模式Prototype,constructor,__proto__详解

    最近由于在找工作,又拿起<JavaScript高级程序设计>看了起来,从中也发现了自己确实还是有很多地方不懂,刚刚看到原型模式这里,今天终于搞懂了,当然,我也不知道自己的理解是否有错. 1 ...

  3. prototype/constructor/__proto__之constructor。

    1.constructor的字面意思就是构造.它是对象的一个属性,它对应的值是该对象的“构造者” //一.构造函数实例化的对象的constructor function Cmf(n,m){ this. ...

  4. prototype constructor __proto__

    constructor, prototype, __proto__ 详解

  5. Js中Prototype、__proto__、Constructor、Object、Function关系介绍

    一. Prototype.__proto__与Object.Function关系介绍 Function.Object:都是Js自带的函数对象.prototype,每一个函数对象都有一个显式的proto ...

  6. 15条规则解析JavaScript对象布局(__proto__、prototype、constructor)

    大家都说JavaScript的属性多,记不过来,各种结构复杂不易了解.确实JS是一门入门快提高难的语言,但是也有其他办法可以辅助记忆.下面就来讨论一下JS的一大难点-对象布局,究竟设计JS这门语言的人 ...

  7. javascript--15条规则解析JavaScript对象布局(__proto__、prototype、constructor)

    大家都说JavaScript的属性多,记不过来,各种结构复杂不易了解.确实JS是一门入门快提高难的语言,但是也有其他办法可以辅助记忆.下面就来讨论一下JS的一大难点-对象布局,究竟设计JS这门语言的人 ...

  8. 【转】Js中Prototype、__proto__、Constructor、Object、Function关系介绍

    一    Prototype.__proto__与Object.Function关系介绍        Function.Object:Js自带的函数对象.         prototype,每一个 ...

  9. 一篇彻底理解JS中的prototype、__proto__与constructor

    1.基本类型不是对象(boolean.undefined.number.string) 2.引用类型都是对象(Array,function ,Object) 3.对象是通过函数创建,并且强调,对象字面 ...

随机推荐

  1. Import larger wordpress xml file

    The maximum size is controlled by two PHP settings: upload_max_filesize, and post_max_size. These ar ...

  2. 对javabean的内省操作

    import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector ...

  3. C#三种判断数据库中取出的字段值是否为空(NULL) 的方法

    操作数据库,需要判断返回的字段值是否为空,收集了3种方法供参考 1 通过System.DBNull判断,网上大部分都使用这个方法. DataTable dt;                     ...

  4. 小程序原理,生成SQL SERVER 2008 数据库所有表的结构文档

    作者:wide288 , 日期:2013-7-31 以前开发中,用 MYSQL 数据库,有个小程序 生成数据库结构文档.很方便,做为开发组的文档很有用. 现在开发中用到了 SQL SERVER 200 ...

  5. Delphi中WebBrowser拦截网页Alert对话框消息(转)

    interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, O ...

  6. Android 仿PhotoShop调色板应用(二) 透明度绘制之AlphaPatternDrawable

    版权声明:本文为博主原创文章,未经博主允许不得转载. Android 仿PhotoShop调色板应用(二) 透明度绘制之AlphaPatternDrawable 这里讲一下如何实现PS调色板中的透明度 ...

  7. linux进程调度之 FIFO 和 RR 调度策略---SYSTEMTAP

    http://blog.chinaunix.net/uid-24774106-id-3379478.html http://blog.chinaunix.net/uid-24774106-id-337 ...

  8. NSNotificationCenter消息机制的介绍

    转载自http://www.cnblogs.com/pengyingh/articles/2367374.html NSNotificationCenter的作用是专门提供程序中不同类之间的消息通讯而 ...

  9. CTE在Oracle和Sqlserver中使用的差异

    CTE是一个很好用的工具,他可以帮助我们清晰代码结构,减少临时表使用,同时oracle和sqlserver都提供支持.但在oracle和sqlserver中使用CTE也存在一定区别. Oracle使用 ...

  10. 9.22 noip模拟试题

    水灾(sliker.cpp/c/pas) 1000MS  64MB 大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没. CCY ...