Javascript 基礎知識(一)

Javascript中有六種數據類型:

字符串 string
數字 number
布爾 boolean
對象 object (包括數組new array、函數function()、時間對象new Data())
空 null
未定義 undefined

Javascript:變量命名的規則(見名知義,駝峰命名法)

1、數字、下劃線、字母、$,組成,數字不能放在開頭
2、不能與關鍵字、保留字重合
3、大小寫敏感

Javascript注釋:

//表示單行注釋
/* */表示多行注釋

Javascript運算符:

number類型類似于Python中的浮點型float,如果要定義為整數類型,需用:parseInt()
沒有整除運算 //是注釋
理解++,--:var a = 3
var b = 3
var c = a++(先把a的值賦給了c,然后在給a+1,這樣,c現在=3,而a=4)
var c = ++b(先給b+1再賦值給c,所以c,b現在都=4)
===(嚴格等): 絕對等于(值和類型均相等)a = 5 那么a == '5'是true
!==(嚴格不等): 不絕對等于(值和類型有一個不相等,或兩個都不相等)a = 5 那么a!== 5 是ture
&&: 短路與 a>0 && a<10 一個不成立就不再往下執行
&: 邏輯與 and
||: 短路或 a<0 || a>10 一個成立就不再往下執行
|: 邏輯或 or
!: 類似于not a = ture !a就代表false a = 100 !!a就代表ture
其他的與python相同
switch 中的 case 是不帶隱藏轉換的 a = 5, switch(a){case'5': ;case 5: ; default: ;}只執行后面case 5

Javascript 中的條件語句:

        if(){};
        if(){}else{};
        if(){}else if{}else{};
        switch(){case: case:  default:}

Javascript 中的循環:

for: for(){}; (){} 中間絕對不能寫 ';'
while: while(){}; (){} 中間絕對不能寫 ';'
do while: do{}while(); 這樣無論如何至少會執行一次

Javascript 中生成隨機數:

Math.random() * ( max - min + 1) + min 表示生成[min,max]的隨機數

Javascript 中定義函數:

function foo(參數){} 多個參數用逗號隔開 參數也可以賦默認值
function foo(a=10, b=20){return a+b;} 返回30

Javascript 中json格式,創建對象的語法

創建對象的字面量方法:

第一種:

var stu1 = {
name:'小張' ,
age: 18,
study: function(courseName){
window.alert(this.name+'正在學習'+courseName)
},
watchAv: function(){
if (this.age >= 18){
window.alert(this.name+'正在看東京冷')
}else{
window.alert(this.name+'只有看熊出沒')
}
}
};
stu1.study('語文');
stu1.watchAv();

第二種:

var stu1 = new object(); new object()是一個函數
stu1.name = '小張';
stu1.age = 18;
stu1.study = function(courseNmae){

}

創建對象的構造器方法 創建同種類型的多個對象:

第一種:

function Student(name, age){
this.name = name;
this.age = age;
this.study = function(courseName){
window.alert(this.name+'正在學習'+courseName)
};
this.watch = function(){
if (this.age >= 18){
window.alert(this.name+'正在看東京冷')
} else {
window.alert(this.name+'只有看熊出沒')
}
};
}
var stu1 = new Student('小張',18);
var stu2 = new Student('jmd',25);
stu1.study('語文');
stu1.watchAv();

第二種:可能更好

function Student(name, age){
this.name = name;
this.age = age;
};
Student.prototye.study = function(courseName){
window.alert(this.name+'正在學習'+courseName);
};
Student.propotype.watch = function(){
if (this.age >= 18){
window.alert(this.name+'正在看東京冷')
} else {
window.alert(this.name+'只有看熊出沒')
}
};
};
var stu1 = new Student('小張',18);
var stu2 = new Student('jmd',25);
stu1.study('語文');
stu1.watchAv();

總結:

在Javascript中函數是第一位(做什么都需要借助函數來完成創建);

Javascript 中自帶的類:方法就是發給類的消息

window window.alert('')
var a = window.prompt('輸入:'); window.alert(a) 彈出輸入的內容
window.comfirm('') 調確認框 window.print()調打印機
window.close() 一般不用,有瀏覽器兼容問題
window.open('url','name'); 一般瀏覽器會自動屏蔽彈窗
重要的:window.location.href=
window.setTimeout(函數,時間) 時間以毫秒為單位

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容