博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript(3)——Object-Oriented Design
阅读量:5115 次
发布时间:2019-06-13

本文共 1863 字,大约阅读时间需要 6 分钟。

自己定义函数

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 codevar 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 codeSmileyFace.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!");

 

转载于:https://www.cnblogs.com/ruruozhenhao/p/8358578.html

你可能感兴趣的文章
挑战练习14.8 删除crime 记录
查看>>
复制文件到U盘错误0x80071AC3,请运行chkdsk并重试
查看>>
duilib之源码分析
查看>>
G_M_C_美食节
查看>>
详细解读MySQL中的权限
查看>>
HDOJ 1698 Just a Hook(线段树成段更新)
查看>>
SQL Server 内存中OLTP内部机制概述(二)
查看>>
Ext.Net学习笔记20:Ext.Net FormPanel 复杂用法
查看>>
php yii框架使用MongoDb
查看>>
cocos2dx中菜单menu 和 menuItem
查看>>
容器List之ArrayList详解
查看>>
GitHub的实现是否是基于此语言的支持网络编程性呢?
查看>>
四、java多线程核心技术——synchronized同步方法与synchronized同步快
查看>>
腾讯PHP工程师面试题两份
查看>>
训练1-U
查看>>
过去5年最受欢迎机器学习论文+代码速查
查看>>
AS3.0 XMLSocket简介
查看>>
列表边框column-rule
查看>>
单机部署RabbltMQ环境的操作记录
查看>>
HTML元素定义 ID,Class,Style的优先级
查看>>