标签:eof 对象 cti 轻量 syntax new middle key 属性
typeof 123; // ‘number‘
typeof NaN; // ‘number‘
typeof ‘str‘; // ‘string‘
typeof true; // ‘boolean‘
typeof undefined; // ‘undefined‘
typeof Math.abs; // ‘function‘
typeof null; // ‘object‘
typeof []; // ‘object‘
typeof {}; // ‘object‘
var n = new Number(123); // 123,生成了新的包装类型
var b = new Boolean(true); // true,生成了新的包装类型
var s = new String(‘str‘); // ‘str‘,生成了新的包装类型
var n = Number(‘123‘); // 123,相当于parseInt()或parseFloat()
typeof n; // ‘number‘
var b = Boolean(‘true‘); // true
typeof b; // ‘boolean‘
var b2 = Boolean(‘false‘); // true! ‘false‘字符串转换结果为true!因为它是非空字符串!
var b3 = Boolean(‘‘); // false
var s = String(123.45); // ‘123.45‘
typeof s; // ‘string‘
var now = new Date();
now; // Wed Jun 24 2015 19:49:22 GMT+0800 (CST)
now.getFullYear(); // 2015, 年份
now.getMonth(); // 5, 月份,注意月份范围是0~11,5表示六月
now.getDate(); // 24, 表示24号
now.getDay(); // 3, 表示星期三
now.getHours(); // 19, 24小时制
now.getMinutes(); // 49, 分钟
now.getSeconds(); // 22, 秒
now.getMilliseconds(); // 875, 毫秒数
now.getTime(); // 1435146562875, 以number形式表示的时间戳
var d = new Date(1435146562875);
d.toLocaleString(); // ‘2015/6/24 下午7:49:22‘,本地时间(北京时区+8:00),显示的字符串与操作系统设定的格式有关
d.toUTCString(); // ‘Wed, 24 Jun 2015 11:49:22 GMT‘,UTC时间,与本地时间相差8小时
var xiaoming = {
name: ‘小明‘,
age: 14,
gender: true,
height: 1.65,
grade: null,
‘middle-school‘: ‘\"W3C\" Middle School‘,
skills: [‘JavaScript‘, ‘Java‘, ‘Python‘, ‘Lisp‘]
};
JSON.stringify(xiaoming);
//‘{"name":"小明","age":14,"gender":true,"height":1.65,"grade":null,"middle-school":"\"W3C\" Middle School","skills":["JavaScript","Java","Python","Lisp"]}‘
JSON.stringify(xiaoming, null, ‘ ‘);
//结果
{
"name": "小明",
"age": 14,
"gender": true,
"height": 1.65,
"grade": null,
"middle-school": "\"W3C\" Middle School",
"skills": [
"JavaScript",
"Java",
"Python",
"Lisp"
]
}
JSON.stringify(xiaoming, [‘name‘, ‘skills‘], ‘ ‘);
//结果
{
"name": "小明",
"skills": [
"JavaScript",
"Java",
"Python",
"Lisp"
]
}
function convert(key, value) {
if (typeof value === ‘string‘) {
return value.toUpperCase();
}
return value;
}
JSON.stringify(xiaoming, convert, ‘ ‘);
//结果:把所有属性值变成大写
{
"name": "小明",
"age": 14,
"gender": true,
"height": 1.65,
"grade": null,
"middle-school": "\"W3C\" MIDDLE SCHOOL",
"skills": [
"JAVASCRIPT",
"JAVA",
"PYTHON",
"LISP"
]
}
var xiaoming = {
name: ‘小明‘,
age: 14,
gender: true,
height: 1.65,
grade: null,
‘middle-school‘: ‘\"W3C\" Middle School‘,
skills: [‘JavaScript‘, ‘Java‘, ‘Python‘, ‘Lisp‘],
toJSON: function () {
return { // 只输出name和age,并且改变了key:
‘Name‘: this.name,
‘Age‘: this.age
};
}
};
JSON.stringify(xiaoming); // ‘{"Name":"小明","Age":14}‘
JSON.parse(‘[1,2,3,true]‘); // [1, 2, 3, true]
JSON.parse(‘{"name":"小明","age":14}‘); // Object {name: ‘小明‘, age: 14}
JSON.parse(‘true‘); // true
JSON.parse(‘123.45‘); // 123.45
JSON.parse(‘{"name":"小明","age":14}‘, function (key, value) {
// 把number * 2:
if (key === ‘name‘) {
return value + ‘同学‘;
}
return value;
}); // Object {name: ‘小明同学‘, age: 14}
eval(string);
//eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码
var xiaoming = ‘{"name":"小明", "age":14}‘;
var jsObj = eval("(" + xiaoming + ")");
标签:eof 对象 cti 轻量 syntax new middle key 属性
原文地址:http://www.cnblogs.com/deng-cc/p/6623606.html