zoukankan      html  css  js  c++  java
  • javascript 设计模式(二)

    (function(){
        
        //在js中我们利用function来定义类
        function Shape(){
            //在类的内部使用var声明的为私有变量
            var x = 1;
            var y = 1;
        };
    
        //利用new关键字进行类的实例化
        var oShape = new Shape();
    
        console.log(oShape.x);//undefined,类的实例化对象中不能访问到私有变量
    
        //如何在类中定义公有变量
        function Parent(){
            this.x = 1;
            this.y = 1;
        };
    
        var oParent = new Parent();
        console.log(oParent.x); //1,在类中使用this关键字来定义公有变量,可以在类的实例化对象中访问到
    
        //定义私有函数
        function Box(){
    
            //私有函数
            var draw1 = function(){
                alert("draw1")
            };
    
            //公有函数
            this.draw2 = function(){
                alert("draw2")
            };
        };
    
        var oBox = new Box();
        // oBox.draw1(); //报错
        oBox.draw2();  //draw2
    
    })();
    
    
    (function(){
    
        //用javascript模仿OOP编程,为什么需要使用OOP编程,因为能够模块化,结构化,方便一个团队共同开发,但是在javascript中没有类的概念,只能模仿
        function Slider(x,y){
            var x = 0;
            var y = 0;
    
            var init = function(){
                x = x;
                y = y;
            };
            init();
        };
    
        var oSlider = new Slider();
    })();
    keep learning
  • 相关阅读:
    【leetcode】1. Two Sum
    【leetcode】32. Longest Valid Parentheses
    【leetcode】20. Valid Parentheses
    scala细节
    【转】CentOS下firefox安装flash说明
    Spring 容器(一)
    源码学习快捷键
    Gradle编译spring3.x报错找不到itextpdf4.2.2解决方案
    Insertion Sort
    Bubble Sort
  • 原文地址:https://www.cnblogs.com/gao-xiong/p/5911026.html
Copyright © 2011-2022 走看看