JS基礎知識

2021-08-21 21:46:58 字數 2709 閱讀 4165

變數型別

值型別和引用型別

var a = 100

var b = a

a =200

console.log(b)//100

var a =

var b = a

b.age = 21

console.log(a.age)//21

引用型別:物件 陣列 函式

內建函式有:object array boolean number string function date regexp error

typeof

typeof

undefined

//nudefined

typeof

'abc'

//string

typeof

123//number

typeof

true

//boolear

typeof {} //object

typeof //object

typeof funtion // function

變數計算

強制型別轉換

json.stringify()

json.parse(『』)

原型和原型鏈

建構函式

fuction foo(name,age)

var f = new foo('zhangsan',20) //建立多個物件

建構函式-拓展

var a = {} 是var a = new object() 的語法糖

var a = {} 是var a = new array() 的語法糖

function foo(){} 是 var foo = new function(){}

使用instanceof 判斷乙個函式是否是乙個變數的建構函式

原型規則

- 所有的引用型別(陣列,物件,函式),都具有物件特徵,即可自由拓展屬性(除了null以外。)

- 所有的引用型別(陣列,物件,函式),都有乙個proto,屬性值是乙個普通的物件。引用型別

- 所有的函式,都有乙個prototype屬性,屬性值也是乙個prototype屬性,屬性值也是乙個普通物件。顯示原型

- 所有的引用型別(陣列,物件,函式),proto屬性值指向它的建構函式「prototype」屬性值。

- 當試圖得到乙個物件的某個屬性時,如果這個物件本身沒有這個屬性,那麼回去他的proto(即他的建構函式的prototype)中尋找。

var obj = {}; obj.a = 100;

var arr = ; arr.a = 100;

function

fn(){}

fn.a = 100;

console.log(obj.__proto__);

console.log(arr.__proto__);

console.log(fn.__proto__);

console.log(fn.prototype);

console.log(obj.__proto__=== object.prototype)

//示例

function

foo(name,age)

foo.prototype.alertname = function

() //建立示例

var f = new foo('zs')

f.printname = function

() f.alertname()

f.printname()

this 永遠指向物件本身。

迴圈物件自身的屬性

for(item in object){}

只想拿到本身的屬性 if(f.hasownproperty(item){}

原型鏈

判斷引用型別屬於哪個建構函式。

如何判斷乙個變數是陣列烈性

var arr =

arr instanceof array //true

繼承列子

function

animal

() function

dog()

}dog.prototype = new animal()

var hashiqi = new dog()

描述new乙個物件的過程

zepto 原始碼中如何使用原型鏈

示例

function

elem

(id)

elem.prototype.html = function

(val)else

}elem.prototype.on = function

(type,fn)

var div1 = new elem('div1')

JS基礎知識

本週抽空學習了一些js新手需要知道的知識 1 js中用 來賦值,例如var a 1.2 迴圈結構和c 基本相同。3 簡單 基本 資料型別 number string boolean undefined null。4 複雜 引用 資料型別 object array date function。還有一些...

js基礎知識

1.ecmascript 直譯器 0,1 幾乎沒有相容性問題 2.dom 文件,物件,模型 document object model 相容性一般 3.bom 瀏覽器 物件,模型 browser object model 完全不相容 number string boolean function ob...

js基礎知識

1.型別和型別轉換 1 值型別 string 字串 number 數值 boolean 布林值 undefined null 2 引用型別 array 陣列 object 物件 function 函式 2.null 和 undefined 1 undefined表示未定義。對於沒有初始化的變數 函式...