zoukankan      html  css  js  c++  java
  • javascript 闭包和原型


    闭包

     function Person(firstName, lastName, age) 
        { 
            
    //私有变量: 
            var _firstName = firstName; 
            
    var _lastName = lastName; 
     
            
    //公共变量: 
            this.age = age; 
     
            
    //方法:  
     
            
    this.getName = function() 
            { 
                
    return(firstName + " " + lastName); 
            }; 
            
    this.SayHello = function() 
            { 
                alert(
    "Hello, I'm " + firstName + " " + lastName); 
            }; 
        }; 
         
        
    var BillGates = new Person("Bill""Gates"53); 
        
    var SteveJobs = new Person("Steve""Jobs"53); 
         
        BillGates.SayHello(); 
        SteveJobs.SayHello(); 
        alert(BillGates.getName() 
    + " " + BillGates.age); 
        alert(BillGates.firstName);     
    //这里不能访问到私有变量     

    原型

        function Circle(x,y,r){   
          
    this.x = x;   
         
    this.y = y;   
         
    this.r = r;   
          
    //this.prototype = null ;     /*这句代码可以看作是隐含存在的,因为javascript 中“类”的定义和函数的定义结构上没有差异,所以可以说,所有函数都隐藏有这样一个属性。*/   
       }  

       Circle.prototype.area 
    = function(){   
         
    return this.r * this.r * 3.14159 ;   
       }  

        
    var circ = new Circle(0,0,2) ;   
        alert(circ.area()) ;  





     
  • 相关阅读:
    详解并发和并行意义
    MoleHill Getting Started AGAL(转)
    解决setInterval计时器不准的问题
    Flash视频播放器开发经验总结
    利用pushState开发无刷页面切换
    用小乐图客抓取人人网照片
    如何在Flash中新窗口打开页面而不被拦截
    响应式开发总结
    用Asroute解决复杂状态切换问题
    对iframe跨域通信的封装
  • 原文地址:https://www.cnblogs.com/gwazy/p/1598046.html
Copyright © 2011-2022 走看看