• JavaScript(3)——Object-Oriented Design


    自己定义函数

    var Winston = function(nickname, age, x, y) {
        this.nickname = nickname;
        this.age = age + "yrs old";
        this.x = x;
        this.y = y;
    };

    引用以定义函数

    var winstonTeen = new Winston("Winsteen", 15, 20, 50);
    var winstonAdult = new Winston("Mr. Winst-a-lot", 30, 229, 50);

     面向对象编程

    /*
    It is very hard for me to do it at the first time
    */
    
    /*
    var faceObj = {
        centerX: 100, 
        centerY: 100
    };
    */
    
    //Above code can be instead by next code
    var SmileyFace = function(centerX, centerY) {
        this.centerX = centerX;
        this.centerY = centerY;
    };
    
    /*
    var drawSmiley = function(faceObj) {
        fill(255, 234, 0);
        ellipse(faceObj.centerX, faceObj.centerY, 150, 150);
        fill(0, 0, 0);
        ellipse(faceObj.centerX-30, faceObj.centerY-30, 20, 20); 
        ellipse(faceObj.centerX+30, faceObj.centerY-30, 20, 20); 
        noFill(); 
        strokeWeight(3);
        arc(faceObj.centerX, faceObj.centerY+10, 64, 40, 0, 180);
    };
    */
    
    //the above code can be instead by next code
    SmileyFace.prototype.draw = function() {
        fill(255, 234, 0);
        ellipse(this.centerX, this.centerY, 150, 150);
        fill(0, 0, 0);
        ellipse(this.centerX-30, this.centerY-30, 20, 20); 
        ellipse(this.centerX+30, this.centerY-30, 20, 20); 
        noFill(); 
        strokeWeight(3);
        arc(this.centerX, this.centerY+10, 64, 40, 0, 180);    
    };
    
    SmileyFace.prototype.speak = function(myString) {
        text(myString, this.centerX-30, this.centerY+90);
    };
    
    var firstFace = new SmileyFace(100, 100);
    var secondFace = new SmileyFace(100, 300);
    var thirdFace = new SmileyFace(300, 300);
    var fourthFace = new SmileyFace(300, 100);
    
    //drawSmiley(faceObj);
    firstFace.draw();
    firstFace.speak("I'm first brother!"); 
    //drawSmiley(secondFace);
    secondFace.draw();
    secondFace.speak("I'm second brother!");
    thirdFace.draw();
    thirdFace.speak("I'm third brother!!!");
    fourthFace.draw();
    fourthFace.speak("I'm fourth brother!");
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    javascript操作cookie实例
    由浅到深了解JavaScript类[转过来的收藏]
    [MySql识记]create utf8 database
    关于游戏开发中的A*/Astar的寻路算法的问题
    对与list<>泛型的一些操作方法
    浅谈完美时空的小气
    npgsql连接postgresql数据库
    哪个美女最漂亮,自己写的js图片自适应切换
    javascript改变this指针
    [图解] 你不知道的 JavaScript “this”(转)
  • 原文地址:https://www.cnblogs.com/h-hkai/p/8358578.html
Copyright © 2020-2023  润新知