zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    top-cpu
    长连接&短连接分析
    HTTP协议详解(头报文)
    exit 与 return 区别
    TOP K 算法
    海量数据处理(面试题&总结)
    SpringBoot和VW-Crawler抓取csdn的文章
    maven项目-修复Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:build-helper-maven-plugin:1.8:add-resource (execution: add-resource, phase: generate-resources) pom.xml报错
    模拟ios应用加载页面
    HTML表格布局
  • 原文地址:https://www.cnblogs.com/h-hkai/p/8358578.html
Copyright © 2011-2022 走看看