2020-12-31 第十七天!!

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Math</title>

<script type="text/javascript">

// var num = Math.random();

// alert(num);//彈出0-1之間的隨機(jī)數(shù)

var a = 1;

var b = 17;

// var num = Math.random()*(b-a)+a;

// alert(num);//彈出10-20之間的隨機(jī)數(shù)

var arr = [];

for(var i=0; i<20; i++){

// var num = Math.floor(Math.random()*(b-a)+a);//向下取整,10-19

var num = Math.floor(Math.random()*(b-a + 1)+a);//向下取整,10-20

arr.push(num);//生成一個(gè)數(shù)就放進(jìn)數(shù)組

}

alert(arr);//17,20,20,11,11,19,17,16,10,11,16,11,18,13,13,11,17,14,19,19

</script>

</head>

<body>

</body>

</html>

單體創(chuàng)建對(duì)象:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>單體創(chuàng)建對(duì)象</title>

<script type="text/javascript">

var Tom = {

// 屬性

name:'tom',

age:18,

// 方法

showName:function(){

alert(this.name);

},

showAge:function(){

alert(this.age);

}

}

//調(diào)用屬性

alert(Tom.name);

alert(Tom.age);

//調(diào)用方法

Tom.showName();

</script>

</head>

<body>

</body>

</html>


<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>單體創(chuàng)建對(duì)象</title>

<script type="text/javascript">

var Tom = {

// 屬性

name:'tom',

age:18,

// 方法

showName:function(){

alert(this.name);

},

showAge:function(){

alert(this.age);

}

}

//調(diào)用屬性

alert(Tom.name);

alert(Tom.age);

//調(diào)用方法

Tom.showName();

</script>

</head>

<body>

</body>

</html>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>工廠模式創(chuàng)建對(duì)象</title>

<script type="text/javascript">

function Person(name,age,job){

//創(chuàng)建一個(gè)空對(duì)象

// var o = new Object();//方式一

var o = {};//方式二

o.name = name;

o.age = age;

o.job = job;

o.showName = function(){

alert(this.name);

}

o.showAge = function(){

alert(this.age);

}

o.showJob = function(){

alert(this.job);

}

return o;

}

var Tom = Person('tom',18,'程序猿');

Tom.showJob();

var Jack = Person('jack',19,'攻城獅');

Jack.showJob();

</script>

</head>

<body>

</body>

</html>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>構(gòu)造函數(shù)</title>

<script type="text/javascript">

function Person(name,age,job){

this.name = name;

this.age = age;

this.job = job;

this.showName = function(){

alert(this.name);

}

this.showAge = function(){

alert(this.age);

}

this.showJob = function(){

alert(this.job);

}

}

//new的作用就相當(dāng)于工廠模式中最開始創(chuàng)建了一個(gè)空對(duì)象,最后把對(duì)象返回

var Bob = new Person('bob',18,'產(chǎn)品汪');

Bob.showJob();

var Alex = new Person('alex',19,'運(yùn)營(yíng)喵');

Alex.showJob();

alert(Bob.showName == Alex.showName);//false

</script>

</head>

<body>

</body>

</html>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>原型模式</title>

<script type="text/javascript">

function Person(name,age,job){

this.name = name;

this.age = age;

this.job = job;

Person.prototype.showName = function(){

alert(this.name);

}

Person.prototype.showAge = function(){

alert(this.age);

}

Person.prototype.showJob = function(){

alert(this.job);

}

}

//先去自己的對(duì)象中找showName函數(shù),再去構(gòu)造函數(shù)的原型找

var Lucy = new Person('lucy',18,'測(cè)試鼠');

//重寫自身對(duì)象中的方法,不會(huì)影響其它對(duì)象

Lucy.showName = function(){

alert('我的名字是' + this.name);

}

Lucy.showName();//我的名字是lucy

var Lily = new Person('lily',19,'市場(chǎng)雞');

Lily.showName();//lily

alert(Lucy.showName == Lily.showName);//false

</script>

</head>

<body>

</body>

</html>

call和apply

<script type="text/javascript">

?/*? call和apply的區(qū)別

?? 二者都可以改變當(dāng)前的this,區(qū)別在于apply方法要將參數(shù)放入數(shù)組中再傳參*/

?? function aa(a,b){

? ? ? alert('我的this是' + this + ',我的a是' + a + ',我的b是' + b);

}

?? aa(2,3);

?? aa.call('abc',2,3);//我的this是abc,我的a是2,我的b是3

?? aa.apply('abc',[2,3])

call和apply都可以調(diào)用函數(shù),可以用修改this??

傳參方式不同 call方法將參數(shù)直接放在? apply方法要將參數(shù)放入數(shù)組中再傳參

</script>

函數(shù)的繼承

<script type="text/javascript">

//父類

?? function Fclass(name, age){

? ? ? this.name = name;

? ? ? this.age = age;

}

?? Fclass.prototype.showName = function(){

? ? ? alert(this.name);

}

?? Fclass.prototype.showAge = function(){

? ? ? alert(this.age);

}

?? //子類

?? function Sclass(name, age, job){

?? //調(diào)用父類的call方法實(shí)現(xiàn)屬性的繼承

? ? ? this.job = job;

}

?? //方法的繼承 (父類的實(shí)例賦給子類的原型)

?? Sclass.prototype = new Fclass();

?? Sclass.prototype.showJob? = function(){

? ? ? alert(this.job);

};

?? var Driver = new Sclass('tom',18,'老司機(jī)')//創(chuàng)建一個(gè)子類的實(shí)例//

?? Driver.showJob();

?? Driver.showAge();

?? Driver.showName();

</script>

新增選擇器

<script type="text/javascript">

window.onload = function(){

?? var div1 = document.querySelector('#div1');

?? console.log(div1);

?? var lis = document.querySelectorAll('.list li');

?? console.log(lis.length);//8

}

<div id="div1">這是一個(gè)div元素</div>

<ul class="list">

?? <li>1</li>

?? <li>2</li>

?? <li>3</li>

?? <li>4</li>

?? <li>5</li>

?? <li>6</li>

?? <li>7</li>

?? <li>8</li>

</ul>

</script>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容