标签:
<script>
//定义空对象
var dog = {};
//对空对象加入方法
dog.name = "Liubo";
//对对象添加方法
dog.getName = function(){
return dog.name;
};
//改变属性和方法
dog.getName = function(){
returen "LaoLiu";
};
//全然删除属性/方法
delete dog.name;
</script>
<script>
var dog = {
name: "LaoLiu",
getName:function(){
return this.name;
}
};
</script>
-2,对象字面量语法a. 将对象包装在大括号里。b. 相应以逗号切割属性和方法。c. 用冒号切割属性名和属性值。d. 当变来那个复制的时候。以右括号结束。
<script>
var Person = function(name){
this.name = name;
this.say = function(){
return "I am " + this.name;
}
}
var adam = new Person("Adam");
console.log(adam.say());
</script>
var Person = function(name){
var this = {};
this.name = name;
this.say = function(){
return "I am " + this.name
};
//return this;
};
<script>
var Objectmark = function(){
this.name = "This is it!";
var that = {};
that.name = "And that is that!";
return that;//由于return that所以导致第一次初始化的name将被忽略
};
var o = new Objectmark();
console.log(o.name);
</script>
<script>
function Man(){
this.name = "Lao Liu";
}
//创建新对象
var laoLiu = new Man();
console.log(typeof laoLiu);// object
console.log(laoLiu.name);//Lao Liu
//反模式
//未使用new操作
var pangLiu = Man();
console.log(typeof pangLiu);//undefined
console.log(pangLiu.name);//Cannot read property ‘name‘ of undefined
</script>
<script>
var jstr = ‘{"mykey" : "my value"}‘;
//反模式
var data = eval(‘(‘ + jstr + ‘)‘);
console.log(data);
//推荐使用
data = JSON.parse(jstr);
console.log(data.mykey);
</script>
<script>
var dog = {
name : "LaoLIu",
dob: new Date(),
legs: [1, 2, 3, 4]
};
var jsonStr = JSON.stringify(dog);
console.log(jsonStr);//{"name":"LaoLIu","dob":"2014-11-07T06:17:28.097Z","legs":[1,2,3,4]}
</script>
<script>
var n = 100;
console.log(typeof n);//number
var nobj = new Number(100);
console.log(typeof nobj);//object
var s = "hello";
console.log(s.toUpperCase());//HELLO
var greet = "1 2 3 4";
console.log(greet.split(" ")[0]);//1
</script>
<script>
try{
throw{
name:"MyError",//自己定义错误类型
message:‘oops‘,
extra: ‘This is Exception‘,
remedy: genericErrorhandler//指定应该处理该错误的函数
};
}catch(e){
alert(e.message);
e.remedy();//调用genericErrorhandler
}
function genericErrorhandler(){
alert("Hi, BigLiu!");
}
</script>
版权声明:本文博客原创文章,博客,未经同意,不得转载。
标签:
原文地址:http://www.cnblogs.com/lcchuguo/p/4681143.html