js實例對象打印(js 打印對象)

本文目錄一覽:

為什麼node.js中有的對象不能夠打印出來

對象只能打印內存地址,具體屬性指定才可以打印具體的對象信息

求一個js方法,能夠打印任何對象所有屬性值。最好有參數可指定子對象的層次。 好用有現金紅包報酬,多

代碼貼上

script

/**

需求1:打印對象裡面所有屬性

需求2:可以指定該對象裡面子對象的打印層次

*/

function isObject(item) {

return Object.prototype.toString.call(item) == ‘[object Object]’;

}

var index = 0;

function printItems(items, n) {

var printStr = ‘{\n’;

for(var item in items) {

if(isObject(items[item]) index n) {

index++;

printStr += item + “:” ;

printStr += printItems(items[item],n);

index = 0;

}else {

printStr += item + ‘:’ + items[item] + ”;

}

printStr += ‘,\n’

}

printStr += ‘\n}’;

return printStr;

}

var n = 3;

var item = {

name: ‘第一層名稱’,

onechild: {name: ‘onchild名稱’,twochild: {name:’twochild名稱’, threechild:{name: ‘threechild名稱’,age:’108′}}},

tchild: {name: ‘onchild名稱’,twochild: {name:’twochild名稱’, threechild:{name: ‘threechild名稱’,age: ‘108’}}},

rnechild: {name: ‘onchild名稱’,twochild: {name:’twochild名稱’, threechild:{name: ‘threechild名稱’,age: ‘108’}}}

}

var print = printItems(item,n);

alert(print);

/script

js怎麼打印object object

json格式有數組和對象2種結構 數組用下標就可以得到,2個中括號括起來的是數組 對象就是用大括號括起來的

js中怎樣打印結果?

能打印,首先你要有一個帶控制台的瀏覽器,比如說chrome、firefox、或者基於chrome內核的瀏覽器,ie9+也可以。

f12打開控制台,切換到“console”選項卡或者叫“控制台”

js中這麼寫

var a = 1;

console.log(a);

這樣程序運行的時候,就會打印到控制台了。不管是數組對象 都能有一個良好的體現

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/249412.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 17:12
下一篇 2024-12-12 17:12

相關推薦

發表回復

登錄後才能評論