2022-03-02——拒绝摆烂第一天(数字API&日期对象&定时器函数)

先写一下之前没记录的函数调用方式:

立即执行函数

立即执行函数:函数定义完,立即被调用,这种函数叫做立即执行函数,立即执行函数往往只会执行一次。

案例演示:

( function(){

alert("我是一个匿名函数");

} ) ();

关于构造函数中call方法

在昨天的构造函数中最后一步中有一个String.call(str),作用是初始化this对象,关于call()方法,这里备注一下:

1.可以调用函数

 function demo(x,y){

console.log("hello world")

}

可通过call()方法来调用 demo.call() 可以直接输出内容

2.(重点) 改变this指向

 var obj = { name: "dj" };

        function Fn(num1, num2) {

            console.log(num1 + num2);

            console.log(this);

            console.log(this.name);

        }

        Fn.call(obj, 100, 200);

        // Fn(1,2);

        // 非严格模式下

        Fn(100, 200);  // this->window num1=100 num2=200

        Fn.call(100, 200);  // this->100 num1=200 num2=undefined

        Fn.call(obj, 100, 200); // this->obj num1=100 num2=200

        Fn.call();  // this->window

        Fn.call(null);  // this->window

        Fn.call(undefined); // this->window


toString方法

String 返回 String 对象的值。

Number 返回 Number 的字符串表示。

Boolean 如果布尔值是true,则返回"true"。否则返回"false"。

Object (默认) 返回"[object ObjectName]",其中 ObjectName 是对象类型的名称。

Array 将 Array 的每个元素转换为字符串,并将它们依次连接起来,两个元素之间用英文逗号作为分隔符进行拼接。

Date 返回日期的文本表示。

Error 返回一个包含相关错误信息的字符串。

Function 返回如下格式的字符串,其中 functionname 是一个函数的名称

此函数的 toString 方法被调用: “function functionname() { [native code] }”

// 字符串

var str = "Hello";

console.log(str.toString());

// 数字

var num = 15.26540;

console.log(num.toString());

// 布尔

var bool = true;

console.log(bool.toString());

// Object

var obj = {name: "张三", age: 18};

console.log(obj.toString());

// 数组

var array = ["CodePlayer", true, 12, -5];

console.log(array.toString());

// 日期

var date = new Date(2013, 7, 18, 23, 11, 59, 230);

console.log(date.toString());

// 错误

var error = new Error("自定义错误信息");

console.log(error.toString());

// 函数

console.log(Function.toString());

okokok,开始记录数字的方法和日期方法

1. 获取数学对象的π(圆周率) 3.14

console.log(Math.PI);

可用于计算弧度

p.s.中小学数学题是真jb难。。。。

2. 向下取整 floor

console.log(Math.floor(0.1));// 0

console.log(Math.floor(-0.1));// -1

console.log(Math.floor(99.99));// 99

3. 向上取整 ceil

console.log(Math.ceil(0.1));// 1

console.log(Math.ceil(-0.1));// -0

console.log(Math.ceil(99.99));// 100

可用于封装动画时计算步长

4. 四舍五入取整 round

console.log(Math.round(0.1));// 0

console.log(Math.round(-0.1));// -1

console.log(Math.round(99.99));// 100

console.log(Math.round(1.4));// 1

console.log(Math.round(1.5));// 2

5. 随机数 random (重点)

console.log(Math.random()); // 0 ~ 1 之间随机小数

console.log(Math.random() * 10);// 0 ~ 10 之间随机数(含小数点)

指定范围的随机数

console.log(Math.random() * 10 + 5);// 5 ~ 10 之间随机数(含小数点)

console.log(Math.random() * (90 - 60) + 60);// 60 ~ 90 之间随机数(含小数点)

var num = Math.random() * (90 - 60) + 60;

console.log(Math.floor(num));// 60 ~ 90之间的随机整数

6. 多少次方 pow

console.log(Math.pow(2,2)); // 2 * 2 = 4

console.log(Math.pow(2,3)); // 2 * 2 * 2 = 8

console.log(Math.pow(5,2)); // 5 * 5 = 25

console.log(Math.pow(5,3)); // 5 * 5 * 5 = 125

7. 平方根

console.log(Math.sqrt(25)); // 5

console.log(Math.sqrt(81)); // 9

console.log(Math.sqrt(5)); // 2.23606797749979

数字方法比较少,但是运用起来是真的麻烦。。我需要练习!!!!!!


日期API

获取日期

然后是关于日期对象,日期对象更是easy,该类方法主要与定时器函数相搭配,先把日期方法统记一哈:

getDate()以数值返回天(1-31)

getDay()以数值获取周名(0-6)在 JavaScript 中,一周的第一天(0)表示“星期日”,即使世界上的一些国家认为周的第一天是“星期一”。

getFullYear()获取四位的年(yyyy)

getHours()获取小时(0-23)

getMilliseconds()获取毫秒(0-999)

getMinutes()获取分(0-59)

getMonth()获取月(0-11)在 JavaScript 中,第一个月(1 月)是月号 0,因此 12 月返回月号 11。

getSeconds()获取秒(0-59)

getTime()获取时间(从 1970 年 1 月 1 日至今)

设置日期

setDate()以数值(1-31)设置日

setFullYear()设置年(可选月和日)

setHours()设置小时(0-23)

setMilliseconds()设置毫秒(0-999)

setMinutes()设置分(0-59)

setMonth()设置月(0-11)

setSeconds()设置秒(0-59)

setTime()设置时间(从 1970 年 1 月 1 日至今的毫秒数)


定时器API函数

1. setInterval

调用者: window

参数: a.匿名函数  b.毫秒

返回值: 数字

功能:每隔一段时间执行一次匿名函

定时器函数

代码一、

var num = 0;

window.setInterval(function(){

    num ++;

    console.log("test",num)

},1000)

代码二、

var count = 0;

var autoPlay = function(){

    count ++;

    console.log("test",count)

}

setInterval(autoPlay,1000);

autoPlay();

代码三、

        var index = 0;

        // 定义变量记录定时器函数

        // var timer = null;

        // 记录当前的定时器函数

        var timer = setInterval(function(){

            index ++;

            // console.log("计数器变量:"+index);

            if(index > 5){

                // 2. 清除定时器函数(停止执行定时器函数)

                clearInterval(timer);

                // 终止代码(不执行后续代码)

                return ;

            }

            console.log("计数器变量:"+index);

        },300)

        // console.log(timer);// 1

2.clearInterval 清除定时器函数(停止执行定时器函数)


晚上20点57,关于我制作时钟时候疏忽的一个问题,就是new Date()赋予的值以及,getSeconds()等获得的值是不会及时更新的,这些值是只保存了静态的时间,所以在制作时钟,使用setInterval()刷新时间时候,需要将这里的值也输入进该函数中。



这里是制作时候随便写的,目前实现的功能会给我在页面刷出来无数个时钟。。

记录两个还没学的东西:innerHTML,getElementById有时间了看一下,感觉就能写出来了

3.3号补充定时器:

3.setTimeout 延迟函数

var d = window.setTimeout(

function(){

console.log("仅仅执行一次");

},1000)

调用者: window

参数:参数1,参数2

参数1是回调函数 ,参数2是毫秒数 ms

返回值:数字1, 用于记录当前的延迟函数

功能:在指定的时间,延迟执行回调函数

这东西用起来没setInterval舒服,而且要实现和后者一样的功能,需要用到递进,代码实现如下:

var index = 0;

        var loop = function(){

            var d = setTimeout(

                function(){

                    index ++;

                    console.log("反复执行:",index);

                    if(index >= 5){

                        clearTimeout(d);

                        return; }

                    loop();

                },100)

        }

loop();

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容