<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="../jQuery/jquery-1.9.1.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
console.info("Hello World!");
var obj = null;
console.info(jQuery.isPlainObject(obj));
obj = {};
console.info(jQuery.isPlainObject(obj));
obj = new Object();
console.info(jQuery.isPlainObject(obj));
//在javascript中,所有数值都是64位双精度的。整数和浮点数之间并没有区别
console.info(typeof 1);
console.info(typeof 1.5);
var thisMonment = new Date();
console.info(thisMonment);
console.info(thisMonment.getFullYear());
try{
throw new Error("something really bad happened");
}catch (e){
console.info(e.name+" : "+ e.message);
}
//一个变量可以进行多次声明
i = 0; //隐式声明
console.info("delete i : " + (delete i));
var i = 0; //显示声明,变量是持久的不能进行删除
console.info("delete i : " + (delete i));
var x; //undefined,默认是undefined
/**
* 各种不同的类型
Error
EvalError
RangeError
ReferenceError
SyntaxError
TypeError
URIError
*/
try{
//函数内外声明了相同的变量,函数体内局部变量将取代全局变量
(function(){
var v1 = "local scope";
v2 = "global scope";
})();
console.info(v1);
console.info(v2);
}catch (e){
console.info(e +" : "+ e.type +" " + e.message);
}
//创建对象
//构造函数
function ZombieObject(name){
this.name = name;
}
var smallZombieObject = new ZombieObject("zhangsan");
//使用字面量创建对象,类似于其他语言的三列(hash)或关联数组
var myObject = {};
var objWithProperties = {
"name" : "zhangsan",
"myObjectAsProperty" : myObject
};
//采用[]访问时属性可以是javascript关键字和变量,.方式不行
console.info(objWithProperties["name"] +" "+objWithProperties.name);
//遍历对象的属性
for(prop in objWithProperties){
console.info(prop +" : "+ objWithProperties[prop]);
}
//基于原型的继承示例
function Monster(type){
this.type = type;
}
Monster.prototype.getType = function(){
return this.type;
}
function Zombie(name){
this.name = name;
}
Zombie.prototype = new Monster();
Zombie.prototype.eatPeople = function(){
console.info("tastes like chicken");
}
var smallZombie = new Zombie();
smallZombie.eatPeople();
console.info(delete Zombie.prototype.eatPeople);
//对象是自知的self-aware,对象知道自己的属性,可以使用hasOwnProperty()方法,返回一个Boolean值
console.info(smallZombie.hasOwnProperty("name"));
console.info(smallZombie.hasOwnProperty("eatPeople"));
//函数没有返回一个特定的值,则它将返回一个undefined值。
console.info((function(){})());
/**
* 函数的两个特性
* 1、函数作为参数传递给其他函数
* 2、匿名函数
*/
function repoter(fn){
console.info("the return value of your function is "+fn());
}
repoter(function(){});
repoter(function(){
return " a simple string";
});
console.info("=========函数=========");
/**
* 自执行函数
* 将函数表达式包装在一对圆括号中[()()],
* 将迫使javascript引擎将function(){}块识别成一个函数表达式,
* 而不是一个函数语句开始
*/
console.info("=========自执行函数=========");
(function(x,y){
console.info("自执行函数:"+(x+y));
})(5,6);
console.info("=========闭包简介=========");
//无论在外部作用域中X的值发生了什么变化,闭包将记住函数执行时变量X的值
var x = 10;
console.info("x = "+x);
var message = (function(param){
return function(){
console.info("闭包中的值:"+ param);
};
})(x);
message(x);
x = 100;
console.info("x = "+x);
message(x);
//eval函数和setTimeOut函数具有自己独立的执行上下文
console.log(this);
/**
* 在javascript中,作用域维持在函数级别而并非块级别。
* 除了不能访问this和参数外(this和参数有调用执行者传入),嵌套函数可以访问外部函数定义的变量
* 闭包:即使在外部函数执行结束后,内部嵌套函数继续保持它对外部函数的引用。闭包还有助于解决命名冲突
* 每次调用一个包裹的函数时,代码虽然没有变换但是javascript将为每一次调用创建一个新的作用域
*/
function getFunction(value){
return function(value){
console.info(this);
return value;
}
}
var a = getFunction(),
b = getFunction(),
c = getFunction();
console.info(a(0));
console.info(b(1));
console.info(c(2));
console.info(a === b);
//闭包保持对外部函数的引用
var obj = {};
obj.method = function(){
var that = this;
this.counter = 0;
var count = function(){
that.counter++;
console.info(that.counter);
}
count();
count();
console.info(this.counter);
return count;
}
var test = obj.method();
//实现私有方法和属性,使用闭包
console.info("=========实现私有方法和属性,使用闭包==========");
function TimeMachine(){
//共有成员
//私有成员
var destination = "2015-05-28";
this.getDestination = function(){
return destination;
}
}
var delorean = new TimeMachine();
console.log(delorean.getDestination());
//访问不到变量
console.log(delorean.destination);
/**
* 使用模块
* 模块模式是一种简单而流行的方式,用于创建自包含的、模块化的代码。
* 创建一个模块,只需要声明一个名称空间、将有关函数绑定于该名称空间,并定义私有成员和专有成员即可
*/
var TIMEMACHINE = {};
TIMEMACHINE.createDelorean = (function(){
//私有成员--感觉这个没有使用
var destination = "";
var model = "";
var fuel = "";
//共有方法
return {
//设置器
setDestination : function(dest){
this.destination = dest;
},
setModel : function(model){
this.model = model;
},
setFuel : function(fuel){
this.fuel = fuel;
},
//访问器
getDestination : function(){
return this.destination;
},
getModel : function(model){
return this.model;
},
getFuel : function(fuel){
return this.fuel;
},
toString : function(){
console.info(this.getModel() + " " + this.getFuel() +" " +this.getDestination());
},
toString2 : function(){
console.info(destination + " " + fuel +" " + model);
}
};
})();
var myMachine = TIMEMACHINE.createDelorean;
myMachine.setDestination("zhangsan");
myMachine.setModel("开挂模式");
myMachine.setFuel("哈哈,不认识这个单词!");
myMachine.toString();
myMachine.toString2();
//json javascript object notation
var list = [1,"2",3,4];
console.info(list);
//删除元素内容,不改变元素长度
delete list[0];
console.info(list);
console.info(list.length);
console.info("=====扩展类型=====");
String.prototype.boolean = function(){
return "true" == this;
}
console.info("false".boolean());
//类型转换
var good = parseInt("010",10);
console.info(good);
var better = + "010";
console.info(better);
//eval()函数可以接受一个字符串,并将视为javascript代码执行。
// 应该限制eval的使用,它容易在代码中引入各种各样严重的问题
$("#submit").click(function(){
$.ajax({
url:"/place2post.php",
type:"post",
success:function(){
//成功代码
}
});
});
});
</script>
</head>
<body>
</body>
</html>