JavaScript入门(二)内置对象,匿名函数

内置对象

<head>
<title>内置对象</title>
<script>



var g='lee喔喔';//Global 全局对象
alert(window.g);//lee喔喔
////Global 全局对象的方法
//1/ URI编码

alert(encodeURI(g));//显示乱码
alert(encodeURIComponent(g));//显示乱码

alert(decodeURI(encodeURI(g)));//lee喔喔

//2. eval()方法,字符串解析


//3. 
alert(Math.PI);
alert(Math.E);
alert(Math.min(1,2,3,4));
alert(Math.max(1,2,3,4));
alert(Math.ceil(3.4));
alert(Math.round(3.7));
alert(Math.floor(4.5));
alert(Math.random()*10+1);//1~10
alert(Math.random()*6+5);//5~10
alert(Math.sqart(9));


alert(Math.sin(0.3));
alert(Math.tan(0.3));


</script>
</head>

面向对象和原型

<head>
<title>面向对象和原型</title>
<script>

var o= new Object();//全局对象
o.name='xiao';
o.age=1;
o.fun=function(){
return this.name + this.age;
};

alert(o.fun());
alert(this.o);//this==window

//工厂模式,集中实例化

function createObject(name,age){
var obj=new Object();
obj.name=name;
obj.age=age;
obj.fun=function(){
return this.name + this.age;
};
return obj;
}

var o1= createObject('xiao',100);
alert(o1.fun());

//构造函数,创建对象

function  Gou(name,age){//构造函数也是函数,第一个字母一般大写,必须使用new Gou创建
this.name=name;//this==gou对象
this.age=age;
this.fun=function(){
	return this.name + this.age;
};

};

var o2=new Gou('xiao',1);
alert(o2.fun());

var o3=new Gou('xiao',1);
alert(o2.fun()==o3.fun());//true  内容相同
alert(o2.fun==o3.fun);//false  函数地址不同

</script>
</head>


<head>
<title>面向对象和原型</title>
<script>


//构造函数,创建对象

function  Gou(name,age){//构造函数也是函数,第一个字母一般大写,必须使用new Gou创建
this.name=name;//this==gou对象
this.age=age;
this.fun=function(){
	return this.name + this.age;
};

};

var o2=new Gou('xiao',1);
alert(o2.fun());

var o3=new Gou('xiao',1);
alert(o2.fun()==o3.fun());//true  内容相同
alert(o2.fun==o3.fun);//false  函数地址不同


//原型,所有的实例共享属性和方法

function Gou1(){}

Gou1.prototype.name='xiao';//原型属性
Gou1.prototype.name=1;//原型属性
Gou1.prototype.fun=function(){
return this.name + this.age;
}

var o4= new Gou1();
alert(o4.fun());

var o5= new Gou1();
alert(o4.fun==o5.fun);//true


</script>
</head>

<head>
<title>面向对象和原型</title>
<script>


//构造函数,创建对象

function  Gou(name,age){//构造函数也是函数,第一个字母一般大写,必须使用new Gou创建
this.name=name;//this==gou对象
this.age=age;
this.fun=function(){
	return this.name + this.age;
};

};

var o2=new Gou('xiao',1);
alert(o2.fun());

var o3=new Gou('xiao',1);
alert(o2.fun()==o3.fun());//true  内容相同
alert(o2.fun==o3.fun);//false  函数地址不同


//原型,所有的实例共享属性和方法

function Gou1(){}

Gou1.prototype.name='xiao';//原型属性
Gou1.prototype.name=1;//原型属性
Gou1.prototype.fun=function(){
return this.name + this.age;
}

var o4= new Gou1();
alert(o4.fun());

var o5= new Gou1();
alert(o4.fun==o5.fun);//true

// 原型,所有的实例共享属性和方法


function Gou2(){}
Gou2.prototype={
name:'xiao',
age:1,
fun:function(){
	return this.name + this.age;
}
};

var o6= new Gou2();
alert(o6.fun());//


// 原型的缺点: 共享, 属性值都一样
</script>
</head>

<head>
<title>面向对象和原型--继承</title>
<script>
//继承,通过原型链实现

function a(){// 基类
this.name='xiao';
}

function b(){//子类
this.age=1;
}

b.prototype=new a();//继承

var bb= new b();
alert(bb.age);
alert(bb.name);
alert(bb instanceof a);//true
alert(bb instanceof b);//true

</script>
</head>

匿名函数

<head>
<title>匿名函数</title>
<script>
//变量就是函数
var f=function(){
return 'nihao';
}

alert(f());


//通过自我执行来执行匿名函数

(function(){//(匿名函数)()
	alert('xiao');
	return 'xiao';
	})();

var f2=(function(){//(匿名函数)() 的返回值
	alert('xiao');
	return 'xiao';
	})();

alert(f2);//xiao 

alert(
(function(){//(匿名函数)() 的返回值
	alert('xiao');
	return 'xiao';
	})()
);


//函数里放匿名函数

function c(){
return function(){
	return 'xiao';
}
}

alert(c()());//xiao
//
var bbb=c();
alert(bbb());//xiao

</script>
</head>



你可能感兴趣的:(JavaScript入门(二)内置对象,匿名函数)