JavaScriptのいくつかの重要な特性
2259 ワード
<script language="javascript">
/*
5 :
1.
2.call()
3.apply()
4.
5.
*/
var Person = function (name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
this.display = function(){
return "I'm name is "+this.name+","+this.age+","+this.sex;
}
}
/*
Person.prototype.display = function(){
return "I'm name is "+this.name+","+this.age+","+this.sex;
}*/
// Person
var MalePerson = function (name,age,sex){
Person.call(this);
this.name = name;
this.age = age;
this.sex = sex;
}
var person = new Person('alex',24,'male');
alert(person.display());
var maleperson = new MalePerson('lucy',18,'female');
alert(maleperson.display());
/*json
XML ,JSON 。 JavaScript Script ,
, JSON 、 、 、 ,
Object 。String,Number Boolean JSON 。 , JSON
String “ abc ”, :“abc”。JSON , [ ] ,
, .Object JSON {} Key-Value
, Object Java Map<String, Object>, Java Class。
Key String 。
*/
var j = [ "abc", 123, false , null];
alert(j[2]);
var json1 = {"name":'alex',"age":24,"sex":'male'};
alert(json1.name+","+json1.age+","+json1.sex);
var json2 = { "name": "Samson" , "address":
{ "city" : "ShangHai" , "street" : "YiJiang Road", "postcode": 202014 }
}
alert(json2.address.city);
/*
" ", .
:1. 2. 。
*/
function f1(){
var n=999;
nAdd = function(){n+=1};
function f2(){alert(n);}
return f2;
}
var result=f1();
result(); // 999
nAdd();
result(); // 1000
</script>