面向对象程序设计

it2025-05-27  11

构造函数都是以大写字母开头,以大写字母开头的不一定是构造函数。

Math对象

比较

1)Math.min():返回一组数中的最小值2)Math.max():返回一组数中的最大值

取整

1)Math.ceil():向上取整

2)Math.floor():向下取整

3)Math.round():四舍五入

随机数

1)Math.random():返回0-1之间的随机数,不包含1

Date 日期对象 构造函数对象

new date=new Date();

参数 0:当前日期

1:设置时间 new Date(2020,9,1); 想要设置2020.10.1

new Date("2020/10/1"); 使用字符串设置月份时,不需要特别修改月份

new Date("2020-10-1");

new Date(毫秒数);

倒计时设计

面向对象

工厂模式

优点:简化代码的操作(封装)

缺点:对象不能细分,共有属性和方法浪费内存

构造函数模式

function Student(name,age,gender){

this.name=name;

this.age=age;

this.gender=gender;

}

var stu1=new Student("lisi",20,"men");

原型对象模式

function Student(name,age,gender){

this.name=name;

this.age=age;

this.gender=gender;}

Student.prortotype.say=function(){}

var stu1=new Student();

var stu2=new Student();

stu1.say();

stu1.say==stu2.say;

继承

让子类的原型指向父类的实例

function Rich(name,age,gender){ this.name=name; this.age=age; this.gender=gender; } Rich.prototype.money=["card1","card2","card3"]; Rich.prototype.enjoy=function(){ alert("enjoy"); } function Poor(name,age,gender,color){ this.name=name; this.age=age; this.gender=gender; this.color=color; } Poor.prototype=Rich.prototype; //Rich.prototype.constructor=Poor; Poor.prototype.work=function(){ alert("work--"); } var p1=new Poor("tom",20,"men","black"); console.log(p1.money); console.log(p1.constructor);

执行结果:

[ 'card1', 'card2', 'card3' ] [Function: Rich]

最新回复(0)