• 关于构造函数什么值传递给他的实例,只有this和prototype


    var a= function (){var bb = 12; this.aa ="xxx"}; 
    a.aa="www"; 
    a.prototype.cc="eee";
    var b = new a; 
    for (var i in b){
    console.log(b[i]);
    }
    console.log (b)
    console.log(b.aa)
    console.log(b.bb)
    

      

    xxx 
    eee 
    a {aa: "xxx", cc: "eee"} 
    xxx
    undefined

     总结,构造函数,传递给实例的是this和prototype定义,其他自身的定义的变量,没用传递给实例
     
    var a= function (){var bb = 12; this.aa ="xxx"}; 
    	 a.aa="www";  
    	  a.prototype = "eee";//1 或者“ss”  new Number(1111) function(){this.cc="ddd"};
    ;
    

      如果将prototype 不赋值为object,而是赋值为原始值,prototype不会传递给实例

    xxx 
    a {aa: "xxx"} 
    xxx 
    undefined

     

    var a= function (){var bb = 12; this.aa ="xxx"}; 
    	 a.aa="www";  
    	 a.prototype = new Number(1111);
    	 var b = new a; 
    	 for (var i in b){
    	 	console.log(b[i]);
    	 }
    	 console.log (b)
    	 console.log(b.aa)
    	 console.log(b.bb)  

    xxx 
    a {aa: "xxx"} 
    xxx 
    undefined

    	var a= function (){var bb = 12; this.aa ="xxx"}; 
    	 a.aa="www";  
    	 a.prototype = new String("11111");
    	 var b = new a; 
    	 for (var i in b){
    	 	console.log(b[i]);
    	 }
    	 console.log (b)
    	 console.log(b.aa)
    	 console.log(b.bb)
    

      

    xxx 
    5undefined 
    a {aa: "xxx", 0: "1", 1: "1", 2: "1", 3: "1", 4: "1"} 
    xxx 
    undefined

    new String,相当于使用数组对象存储的,但是遍历不出来值。

    var a= function (){var bb = 12; this.aa ="xxx"}; 
    	 a.aa="www";  
    	 a.prototype = [1,2];
    	 var b = new a; 
    	 for (var i in b){
    	 	console.log(b[i]);
    	 }
    	 console.log (b)
    	 console.log(b.aa)
    	 console.log(b.bb)
    

      

    xxx 


    [aa: "xxx", 0: 1, 1: 2] 
    xxx 
    undefined

    prototype就是{},实例化的对象,key value形式的,其他的都不行,不做传递。

    var a=[1,2,3];
    	  for(var i in a){
    		  console.log(typeof i);
    		  console.log(a[i]);
    		  
    	  }	
    

      

    string 

    string 

    string 

    3

    数组的下标和对象一样,都是stirng

     var a=[1,2,3];
    	  for(var i in a){
    		  console.log(typeof i);
    		  console.log(a[i]);
    		  
    	  }	
    	  var b={0:a,1:b}
    	  console.log(b[0])
    	  console.log(b[1])
    	
    

      

    string 

    string 

    string 

    [1, 2, 3] 

    undefined

    这就是数组的基本方式,

    在对象中,如果是数组方式当key,不能使用b.1或者b."1",会报语法错误,跟array保持一致了

    b[0]===b["0"] 为ture

     总结:

    只有this,prototype的值传递给实例,其中prototype必须是key-value的形式才传递,就是实例化后的,

    数组和object的关系就是都一致的。 

  • 相关阅读:
    【转】IBatis.Net项目数据库SqlServer迁移至Oracle
    【转】远程桌面 剪切板复制文件失效解决方法
    为什么越学反而越蠢?碎片化学习是个骗局
    Spring MVC起步
    [1-1] 把时间当做朋友(李笑来)Chapter 1 【心智的力量】 摘录
    Java反射总结
    No enclosing instance of type Demo is accessible. Must qualify the allocation with an enclosing instance of type Demo (e.g. x.new A() where x is an instance of Demo).
    hiberbnate 缓存策略概述
    一分钟看你缺乏哪种维生素
    zrrx笔试题(一)——文件复制&日期天数差
  • 原文地址:https://www.cnblogs.com/danghuijian/p/3857505.html
Copyright © 2020-2023  润新知