javascript语言核心
一、标识符
javascript标识符必须以字母、下划线(_)、美元符($)开始。后续字符可以是字母、数字、下划线、美元符。(数字不允许作为首字符出现,以便javascript可以轻区分开标识符和数字)。
二、可选的分号
使用分号来明确标记语句的结束。
三、语句
1.debugger 语句 // 这条语句产生一个断点
2."use strict" //表示后续的代码会解析为严格代码
判断是否支持严格模式:
var hasStrictMode=(function(){ "use strict"; return this===undefined; }());
四、对象属性
1.检测属性
in // 对象的自有属性或者继承属性中包含这个属性则返回true;
hasOwnProperty(); // 检测是否是对象的自有属性,是则返回true;
propertyIsEnumerable(); // 是hasOwnProperty()的增强版,只有是对象的自有属性并且这个属性可枚举性(enumerable attribute)为true时它才返回true;
注:ECMAScript 5 定义了两个可以用以枚举属性名称的函数:
Object.keys(); // 返回对象中可枚举的自有属性的名称数组。
Object.getOwnPropertyNames(); // 返回对象所有自有属性数组(包含不可枚举自有属性)。
2.属性getter和setter
var p={
x:1.0,
y:1.0,
get r(){ return Math.sqrt(this.x * this.x + this.y * this.y); },
set r(newvalue){
var oldvalue= Math.sqrt(this.x * this.x + this.y * this.y);
var ratio=newvalue/oldvalue;
this.x *=ratio;
this.y *=ratio;
}
};
3.对象属性的特性 (4个)
value // 值
writable // 可写性
enumerable // 可枚举性
configurable // 可配置性
方法:
Object.getOwnPropertyDescriptor({x:1},"x"); // 获取对象指定属性的特性描述符,结果为:{ value:1, writable:true, enumerable:true, configurable:true };
Object.getPrototypeOf(); // 获取继承属性的特性
Object.definePeoperty(); // 设置属性的特性
例:
var o={};
Object.defineProperty(o,"x",{
value:1,
writable:true,
enumerable:false,
configurable:true
});

浙公网安备 33010602011771号