重读js高程笔记二
引用類型的值(對象)是引用類型的一個實例,在ES中引用類型是一種數據結構,將數據和功能組織在一起。引用類型有時候也被稱之為對象定義,因為他們描述的是一類對象所具有的屬性和方法。
Object類型
兩種創建方式
1 new Object()
var person = new Object(); person.name = "jing"; person,age = 20;2 對象字面量
var person = {
name:"jing",
age:20
}
屬性的訪問兩種方式,1-person.name,2-person['name'],后一種方式的好處可以用來在代碼中拼接屬性名。
Array
創建方式
// 使用new Array() 構造函數 var arr1 = new Array(); var arr2 = new Array(20); // 長度為20 var arr3 = new Array("red","blue","yellow"); // 數組字面量表示法 var arr4 = ["red","blue","yellow"];檢測數組方法
- arr instanceof Array,頁面多個框架引起的多個全局執行環境問題。怎么理解呢(?)
- ES5引入的Array.isArray(arr)解決上面的問題 #### Array的常用方法
- 轉換方法(所有對象都具有),toString()/toLocalString()/ValueOf()
- 棧方法,push()
- 隊列方法,shift() unshift()-添加元素
- 重排序,reverse()和sort() (?)
- 操作方法,concat() slice()和splice()-刪除/插入/替換
- 位置方法,indexOf() lastIndexOf()
- 迭代方法,every()/filter()/forEach()/map()/some()
Date
創建方法
var date1 = new Date(args);常見使用方法
regExp
regExp是ES支持正則表達式的一個接口
正則表達式,用得好可以少寫很多邏輯代碼
Function
Boolean
var falseObj = new Boolean(false); var falseValue = false;console.log(falseObj && true); // trueconsole.log(falseValue && true); // falseconsole.log(typeof falseObj); // Objectconsole.log(typeof falseValue); // booleanconsole.log(falseObj instanceof Boolean); // trueconsole.log(falseValue instanceof Boolean) // false- Boolean類型是與布爾值對應的引用類型
- 在布爾表達式中使用布爾對象
Number
與數字對應的引用類型,幾個好用的方法
var num = 10; console.log(num.toString(2)); console.log(num.toString(8)); // 加入參數轉 顯示其他進制console.log(num.toFixed(2)); // 保留幾位小數String
String是字符串的對象包裝類型
一些常用的方法如下
- charAt()/charCodeAt() 找到第幾位字符
- slice()/substr()/substring() 截取 字符串,注意差別
- indexOf()/lastIndexOf() 找到位置
- trim()ES5引入的,創建一個字符串副本,刪除前后的空格返回
- toUpperCase()/toLowerCase() 大小寫轉換
- 模式匹配本質上是調用了RegExp的exec(),有match()/search()/replace()/split()
- localeCompare() 比較兩個字符串
- str.fromCodeAt(104,101,108,108,101) //hello 傳入多個字符編碼轉換成一個字符串
單體內置對象
Global對象
- enCodeURI()/enCodeURIComponent() URI編碼方法
- eval() 強大到直接執行語句
- window對象
Math對象
- min()/max()
- ceil()/floor()/round() 向上向下標準舍入
- random() 返回介于0和1之間的隨機數
- 其他abs/sqrt/...
轉載于:https://www.cnblogs.com/lifesimple/p/zhong-dujs-gao-cheng-bi-ji-er.html
總結
- 上一篇: 热烈祝贺Polymer中文组织站点上线
- 下一篇: iOS开发知识点总结