「javascript高級プログラム設計」学習ノート5.6.基本包装タイプ
12276 ワード
前端の民謡のgithbに注目して、もっと多くのオリジナル技術文章を読みましょう.
基本包装の種類
関連コード→ 3つの特別な参照タイプ は、基本タイプ値を読み出す毎に、バックグラウンドに対応する基本パッケージタイプのオブジェクト を作成する.上記のステップは、 にも適用される.引用タイプと基本包装タイプの主な違いは対象の生存期間にあります. 引用タイプ:スコープを離れる前に、オブジェクトはずっとメモリの中にあります. 基本包装タイプ:実行が完了したら、すぐに廃棄されます.
基本包装タイプの例に対して、typeofを呼び出したら「Object」に戻ります.すべての基本包装タイプのオブジェクトはブール値true に変換されます. Object構成関数は、着信値の種類に応じて基本的なパッケージタイプの例 を返す.は、 を直接呼び出します.
戻り値
valueOf()
toString()基本タイプのブール値vsは、タイプのブール値を参照する. typeofオペレータは、基本タイプについてbootleanに戻り、参照タイプについては、Object に戻る. instanceオペレータは、基本タイプに対してfalseを返し、参照タイプに対してtrue を返します.
戻り値
valueOf()
toLocale String()
toString()は、toString()メソッドに基数を表すパラメータを伝達し、いくつかの進数を返す文字列形式 を教える.
戻り値
toFixed(num)
toExponential(num)
トークシシオン(num)基本タイプの数値vs参照タイプの数値: typeofオペレータは、基本タイプにnumberを返し、参照タイプに対しては、object を返します. instanceオペレータは、基本タイプに対してfalseを返し、参照タイプに対してtrue を返します.
戻り値
valueOf()
toLocale String()
toString() Stringタイプは、各例において、length属性があり、文字列に含まれる文字の数を表し、2節の文字も1文字 としてカウントされる.
戻り値
charAt(num)
位置を指定した文字
charCodeAt()
指定された位置の文字コード
戻り値
concat()
スティッチングした文字列
slice(num 1,num 2)
操作された文字列のサブ文字列
substring(num 1,num 2)
操作された文字列のサブ文字列
substr(num 1,num 2)
操作された文字列のサブ文字列
戻り値
indexOf()
サブ文字列の位置、前→後検索
lastIndexOf()
サブ文字列の位置、後→前検索
戻り値
trim()
コピーを作成し、前のスペースを削除し、コピーを返します.
trimLeft()
コピーを作成し、前のスペースを削除し、コピーを返します.
trimRight()
コピーを作成し、末尾のスペースを削除し、コピーを返します.
戻り値
LowerCase()
小文字をまわす
toLocaree LowerCase()
地域によって小文字にする
toUpperCase()
大文字に変換
toLocaleUpperCase()
地域ごとに大文字に変換
戻り値
match()
行列
search()
最初のマッチ項目の索引は、-1を返しませんでした.
replace()
置換後の文字列
スプリット()
複数の文字列に分割して、配列に配置します.
戻り値
locale Compare()
文字列パラメータのアルファベットの前後関係
戻り値
from CharCode()
変換後の文字列
戻り値
アンチョー(name)基本包装のタイプは、どのような特別な参照のタイプが含まれていますか?読み取り時のバックグラウンドの手順は何ですか? 引用タイプと基本包装タイプの違いは何ですか? 基本包装タイプの例に対してtypeofを呼び出したら何を返しますか?基本包装のタイプの対象は全部変換されます.なぜですか? 基本包装タイプをObjectコンストラクタを使って判断してください. newを使用して基本包装タイプの構造関数を呼び出し、同名の変換関数を直接呼び出したのとの違いは何ですか? Booleanの引用タイプはどのような方法を書き換えましたか?その基本タイプと引用タイプの違いは何ですか? Numberの引用タイプはどのような方法を書き換えましたか?どのような文字列の書式設定方法がありますか?その基本タイプと引用タイプの違いは何ですか? Stringの引用タイプはどのような方法を書き換えましたか?どの文字列の操作や解析方法が提供されていますか?その基本タイプと引用タイプの違いは何ですか? .charAt()、charCodeAt()、concat()、slice()、substr()、substring()、indexOf()、lastIndexOf()、trim()、tolowerCase()、toUpperCase()、match()、search、search(palotch)、24)などの意味を説明してください.
基本包装の種類
関連コード→
Boolean
、Number
、String
var s1 = 'some text'
var s2 = s1.substring(2) // ,
// :
var s1 = new String('some text') // 1. String
var s2 = s1.substring(2) // 2.
s1 = null // 3.
Boolean
およびNumber
タイプvar s1 = 'some text'
s1.color = 'red' // String
console.log(s1.color) // undefined,s1 , String , color
var objText = new Object('some text') // String
console.log(objText instanceof String) // true
var objBoolean = new Object(false) // Boolean
console.log(objBoolean instanceof Boolean) // true
var objNumber = new Object(123) // Number
console.log(objNumber instanceof Number) // true
console.log(objNumber instanceof Boolean) // false
new
を使用して、基本包装タイプの構造関数vsを呼び出し、同名の変換関数var value = '25'
var number = Number(value) // , Number
console.log(typeof number) // number
var obj = new Number(value) // , Number
console.log(typeof obj) // object
Booleanタイプvar booleanObject = new Boolean(true) // Boolean , true/false
書き換え(引き継ぎ)の方法戻り値
valueOf()
Boolean
true/falsetoString()
String
「true」/「false」var falseObj = new Boolean(false) // falseObj , true( true)
var falseValue = false // falseValue , false
console.log(falseObj && true) // true
console.log(falseValue && true) // false
console.log(typeof falseObj) // object,
console.log(typeof falseValue) // boolean,
console.log(falseObj instanceof Boolean) // true,Boolean Boolean
console.log(falseValue instanceof Boolean) // false
Numberタイプvar numberObject = new Number(10) // Number ,
書き換え(引き継ぎ)の方法戻り値
valueOf()
Number
値toLocale String()
String
文字列形式の数値toString()
String
文字列形式の数値var num1 = 10
console.log(num1.toString()) // ,"10"
console.log(num1.toString(2)) // ,"1010"
console.log(num1.toString(8)) // ,"12"
console.log(num1.toString(10)) // ,"10"
console.log(num1.toString(16)) // ,"a"
文字列の書式設定方法戻り値
toFixed(num)
String
指定小数位toExponential(num)
String
指数表示法トークシシオン(num)
String
はfixedまたはexponentialフォーマットを返します.var num2 = 10000
console.log(num2.toFixed(2)) // ,"10000.00"
console.log(num2.toExponential(2)) // ,"1.00e+4"
var num3 = 99
console.log(num3.toPrecision(1)) // ,"1e+2"
console.log(num3.toPrecision(2)) // ,"99"
console.log(num3.toPrecision(3)) // ,"99.0"
var numberObject = new Number(10)
var numberValue = 10
console.log(typeof numberObject) // object,
console.log(typeof numberValue) // value,
console.log(numberObject instanceof Number) // true,Number Number
console.log(numberValue instanceof Number) // false
Stringタイプvar StringObject = new String('hello world') // String ,
書き換え(引き継ぎ)の方法戻り値
valueOf()
String
文字列の値toLocale String()
String
文字列の値toString()
String
文字列の値console.log(stringObject.length) // "11"
文字の使い方戻り値
charAt(num)
位置を指定した文字
charCodeAt()
指定された位置の文字コード
console.log(stringObject.charAt(1)) // "e"
console.log(stringObject.charCodeAt(1)) // "101"
console.log(stringObject[1]) // "e"
文字列の操作方法戻り値
concat()
スティッチングした文字列
slice(num 1,num 2)
操作された文字列のサブ文字列
substring(num 1,num 2)
操作された文字列のサブ文字列
substr(num 1,num 2)
操作された文字列のサブ文字列
console.log(stringObject.concat(' wonderful')) // "hello world wonderful"
console.log(stringObject.slice(3)) // "lo world"
console.log(stringObject.substring(3)) // "lo world"
console.log(stringObject.substr(3)) // "lo world"
console.log(stringObject.slice(3, 7)) // "lo w", ( )
console.log(stringObject.substring(3, 7)) // "lo w", ( )
console.log(stringObject.substr(3, 7)) // "lo worl",
console.log(stringObject.slice(-3)) // "rld",-3 11-3=8
console.log(stringObject.substring(-3)) // "hello world",-3 0
console.log(stringObject.substr(-3)) // "rld",-3 11-3=8
console.log(stringObject.slice(3, -4)) // "lo w",-4 11-3=7
console.log(stringObject.substring(3, -4)) // "lo w",-4 0,substring(3,0) substring(0,3)
console.log(stringObject.substr(3, -4)) // "",-4 0,
文字列の位置方法戻り値
indexOf()
サブ文字列の位置、前→後検索
lastIndexOf()
サブ文字列の位置、後→前検索
console.log(stringObject.indexOf('o')) // 4
console.log(stringObject.lastIndexOf('o')) // 7
console.log(stringObject.indexOf('o', 6)) // 7,
console.log(stringObject.lastIndexOf('o', 6)) // 4,
var loremString =
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
var position = new Array()
var pos = loremString.indexOf('o')
while (pos > -1) {
position.push(pos)
pos = loremString.indexOf('o', pos + 1)
}
console.log(position) // o
trim()メソッド戻り値
trim()
コピーを作成し、前のスペースを削除し、コピーを返します.
trimLeft()
コピーを作成し、前のスペースを削除し、コピーを返します.
trimRight()
コピーを作成し、末尾のスペースを削除し、コピーを返します.
var stringValue = ' hello world '
var trimStringValue = stringValue.trim()
console.log(stringValue) // " hello world "
console.log(trimStringValue) // "hello world"
文字列の大文字小文字変換方法戻り値
LowerCase()
小文字をまわす
toLocaree LowerCase()
地域によって小文字にする
toUpperCase()
大文字に変換
toLocaleUpperCase()
地域ごとに大文字に変換
console.log(stringObject.toUpperCase()) // "HELLO WORLD"
console.log(stringObject.toLocaleUpperCase()) // "HELLO WORLD"
console.log(stringObject.toLowerCase()) // "hello world"
console.log(stringObject.toLocaleLowerCase()) // "hello world"
文字列パターンマッチング方法戻り値
match()
行列
search()
最初のマッチ項目の索引は、-1を返しませんでした.
replace()
置換後の文字列
スプリット()
複数の文字列に分割して、配列に配置します.
var text = 'cat, bat, sat, fat'
var pattern = /.at/
var matches = text.match(pattern)
console.log(matches) // [ 'cat',index: 0,input: 'cat, bat, sat, fat',groups: undefined ]
// match , ,
console.log(matches.index) // 0
console.log(matches[0]) // "cat"
console.log(pattern.lastIndex) // 0
var pos = text.search(/at/)
console.log(pos) // 1, "at"
var result = text.replace('at', 'ond')
console.log(result) // "cond, bat, sat, fat",
result = text.replace(/at/g, 'ond')
console.log(result) // "cond, bond, sond, fond",
result = text.replace(/(.at)/g, 'word($1)')
console.log(result) // "word(cat), word(bat), word(sat), word(fat)",$1
function htmlEscape(text) {
return text.replace(/[<>"&]/g, function (match, pos, originalText) {
switch (match) {
case '':
return '>'
break
case '&':
return '&'
break
case '"':
return '"'
break
}
})
}
console.log(htmlEscape('Hello world!
')) // "<p class="greeting">Hello world!</p>"
console.log(text.split(',')) // [ 'cat', ' bat', ' sat', ' fat' ]
console.log(text.split(',', 2)) // [ 'cat', ' bat' ],
console.log(text.split(/[^\,]+/)) // [ '', ',', ',', ',', '' ], , ,
locale Compare()方法戻り値
locale Compare()
文字列パラメータのアルファベットの前後関係
var stringCompare = 'yellow'
console.log(stringCompare.localeCompare('brick')) // 1,brick yellow
console.log(stringCompare.localeCompare('yellow')) // 0,yellow yellow
console.log(stringCompare.localeCompare('zoo')) // -1,zoo yellow
function determineOrder(value) {
var result = stringCompare.localeCompare(value)
result < 0 &&
console.log(`The string 'yellow' comes before the string ${value}.`)
result > 0 &&
console.log(`The string 'yellow' comes after the string ${value}.`)
result === 0 &&
console.log(`The string 'yellow' is equal to the string ${value}.`)
}
determineOrder('brick') // "The string 'yellow' comes after the string brick."
determineOrder('yellow') // "The string 'yellow' is equal to the string zoo."
determineOrder('zoo') // "The string 'yellow' comes before the string zoo."
from CharCodeの方法戻り値
from CharCode()
変換後の文字列
// , charCodeAt()
console.log(String.fromCharCode(104, 101, 108, 108, 111)) // "hello"
htmlメソッド戻り値
アンチョー(name)
ストリングス
big()string
bold()string
fixed()string
fontカラーstring
fontsizestring
italics()string
linkストリングス
small()string
ストリップ()string
sub()string
sup()string
まとめ&質問