js物件高階

2021-10-24 01:21:46 字數 2241 閱讀 4052

使用建構函式來建立物件,建構函式相當於乙個模板。模板就是所有建立的物件的型別。

instanceof 判斷某個物件是否屬於某個型別。

js特性:基本資料型別可以訪問與之對應的物件型別的屬性和行為,在訪問時,自動將基本資料類 型轉換為物件型別。

string -> string number -> number boolean -> boolean

string物件常用方法:

var a="你好嗎?我不好,為什麼不好?";

console.log(typeof a);

console.log(a.replace(/不好/g,"好"));

console.log(a.length);

//返回目標字串指定位置的字元。

console.log(a.charat(1));

// 與目標字串進行拼接。

console.log(a.concat("love"));

//:是否以指定的字串開頭或結尾。

console.log(a.startswith("你好"));

console.log(a.endswith("好?"));

//返回子字串在當前字串第一次出現的位置,如果存在,則大於等於0,如果不存 在,返回-1。

//當指定第二引數的時候,表示從哪個位置開始查詢。

console.log(a.indexof("我",2));

console.log(a.lastindexof());

//按照指定的分隔符將字串分割成乙個陣列。常用。

console.log(a.split("好"));

//擷取字串,從a開始擷取,一共擷取b個字元。

console.log(a.substr(5,2));

//擷取字串。從a擷取到b,包含a,但不包含b

console.log(a.substring(5,7));

//將字串轉換成小寫,及大寫

var b="djsh"

console.log(b.tolowercase());

console.log(b.touppercase());

console.log(a);

//去掉字串兩端的空白。

console.log(a.trim());

console.log(a.trimleft());

console.log(a.trimright());

陣列的常用方法

var a=[1,30,2,3,21,32];

var b=new array(1,2,3);

console.log(a,b);

console.log(a.length);

//與目標陣列進行拼接。返回乙個新的陣列,原陣列不變。

console.log(a.concat("wo"));

//,對陣列中的每乙個元素進行遍歷處理。引數是乙個函式,此函式用於對每個數 組元素進行處理。

a.foreach(function(item,index));

//.indexof和lastindexof : 在陣列中查詢指定的元素,找到則返回其索引,找不到返回-1。

//支援第2個引數,如果有第2個引數,表示從指定的第2個引數的位置開始查詢。

console.log(a.indexof(2,0));

//join 函式是 split 函式的逆操作。將陣列元素使用指定的分隔符連線起來

console.log(a.join(":"));

// push 在陣列的尾部新增乙個值。

// a.push(10);

//. pop 在陣列的尾部彈出乙個值,作為函式返回值,同時在陣列中將此元素刪除。

// console.log(a.pop(10));

console.log(a);

//將陣列元素首位顛倒

// var a=["我","愛","你"]

console.log(a.reverse());

//. unshift 在陣列的頭部新增乙個值。

a.unshift(20);

console.log(a);

// 在陣列的頭部彈出乙個值,作為函式返回值,同時在陣列中將此元素刪除。

console.log(a.shift());

console.log(a);

//對陣列的元素進行排序。引數是乙個函式。

// a.sort(function(a,b){

// return a

JS建立物件 高階

雖然用object建構函式或物件字面量可以建立單個物件,但有乙個明顯的缺點 使用同乙個介面建立物件,會產生大量重複的 為此紅寶書中又有三種基本的建立物件的方式 工廠模式 function createperson name,age,job return o var person1 createper...

js物件導向高階

function student name,age var stu1 new student 張三 18 像這樣需要通過new來使用的函式都可以稱為建構函式 通過this寫進去的,是它的例項方法,可以通過new來呼叫 student.nation china 向這樣直接新增進去的是靜態成員,只能通過...

Js物件高階程式

物件定義為 無序屬性的集合,其屬性可以包含基本值 物件或者函式 物件是由屬性和方法組成的 是乙個無序鍵值對的集合,指的是乙個具體的事物 let obj new object obj.name jack obj.getname function 通過 new 建立的物件,都會在記憶體的堆空間中開啟新的...