您的位置:首页 > Web前端 > JavaScript

17、JavaScript之工厂方式定义对象

2013-04-26 16:27 579 查看
1、每个函数对象都有一个length属性,表示该函数期望接收的参数格式。它与函数的arguments不同,arguments.length表示函数实际接收的参数个数。

<html>
<head>
<script type="text/javascript">

var add = function(num)
{
alert(num);
}

alert(add.length);

var add1 = function(num,num2)
{
alert(num + num2);
}
alert(add1.length);
var add2 = function(num,num2,num3)
{
alert(num + num2 + num3);
}
alert(add2.length);

</script>
</head>

<body>

</body>
</html>

结果:1  、2  、 3

2、JavaScript中有五种原始数据类型:Undefined、Null、Boolean、Number以及String

      Undefined数据类型的值只有一个:undefined。

      Null数据类型的值只有一个:null

      Boolean数据类型的值有两个:true 和 false

      JavaScript中没有char数据类型的。

      typeof是一元运算符,后跟变量的名称,用于获取变量的数据类型,其返回值有5个:undefined、boolean、number、string以及object

      在JavaScript中,如果函数没有声明返回值,那么会返回undefined。

      null与undefined的关系:undefined实际上是从null派生出来的。

      强制类型转换:在Javascript中有三种强制类型转换:Boolean(value),Number(value),String(value)。

      有new是一个对象,没有new,是强制类型转换

      在JavaScript中,对于函数中定义的变量来说,加var表示局部变量,不加var表示全局变量。

      在JavaScript中,所有对象都是从Object对象继承过来的。Object中的属性是不可枚举的(object.PropertyIsEnumerable("prototype")是false的),因此无法通过for...in语句得到其中的属性的

      在javascript中,可以动态的对象的属性,可以动态的删除对象的属性。

      在Javascript中定义对象的第二种方式var object2 = {username:"zhangs",password:"123"};花括号内的用分号分隔键值对用

      对于JavaScript数组的sort方法来说,他会先将待排序的内容转换为字符串(调用toString()方法)按照字符串的先后顺序进行排序

     

<html>
<head>
<script type="text/javascript">

var s;
alert(s);  //undefined

var n = "hello"; //n是原始类型
alert(typeof n); //String

var m = new String("hello");//m是对象类型
alert(typeof m); //object

var o = false;
alert(typeof o);//boolean

var p = 3;
alert(typeof p); //number

function add()
{
return 3;
}
alert(add()); //3

function add1()
{
}
alert(add1());//undefined

function add2()
{
return;
}
alert(add2());//undefined

alert(typeof q);//undefined

alert(undefined == null);//true

var r = Number(3);
alert(typeof r);//number

var s = Boolean("hello");
alert(s);//true
var s1 = Boolean(true);
alert(s1);//true
var s2 = Boolean();
alert(s2);//false

var t =new String("hello");
alert(typeof t);//object
var t1 =String("hello");
alert(typeof t1);//string

var u = new Boolean("hello");
alert(u);//true

var object = new Object();
for(var v in object)
{
alert(v);  //object中的属性无法枚举出来,所以没有显式
}

alert(object.propertyIsEnumerable("prototype")); //false
//判断属性是否可枚举

for(var v in window)
{
alert(v);
}

</script>
</head>

<body>

</body>
</html>


 

var object1 = new Object();
alert(object1.username); //undefined

object1.username = "zhangsan"; //动态为生成对象添加属性
//也可以这样:object1["username"] = "zhangsan";
alert(object1.username);//zhangsan

delete object1.username;//username属性从object1删除
alert(object1.username); //undefined

var object2 = {username:"zhangs",password:"123"};//定义对象的另一种方式,最常用的方式
alert(object2.username);


     

<script type="text/javascript">
/*
var array = new Array();
array.push(1);
array.push(2);
array.push(3);
alert(array.length);

var array = [1,3,25];
array.sort();//sort
alert(array);//结果为[1,25,3]
*/

function compare(num1,num2)
{
var temp1 = parseInt(num1);
var temp2 = parseInt(num2);

if(temp1 < temp2)
{
return -1;
}
else if(temp1 == temp2)
{
return 0;
}
else if(temp1 > temp2)
{
return 1;
}
}

var array = [1,3,25];
/*
array.sort(compare);//函数名是对象引用
alert(array);//结果为:[1,3,25]
*/
array.sort(function(num1,num2)
{
var temp1 = parseInt(num1);
var temp2 = parseInt(num2);

if(temp1 < temp2)
{
return -1;
}
else if(temp1 == temp2)
{
return 0;
}
else if(temp1 > temp2)
{
return 1;
}
});//使用匿名函数
alert(array);
</script>


3、JavaScript中定义对象的几种方式(javascript中没有类的概念,只有对象的概念):

    1)基于已有对象扩充其属性和方法:

<html>
<head>
<script type="text/javascript">
var object = new Object();
object.name = "zhangsan";
object.sayName = function(name)
{
this.name = name;
alert(this.name);
}

object.sayName("lisi");
</script>
</head>

<body>

</body>
</html>


    2)工厂方式

<html>
<head>
<script type="text/javascript">
//工厂方式创建对象
function createObject()
{
var object = new Object();

object.username = "zhang";
object.password = "123";

object.get = function()
{
alert(this.username + "," + this.password);
}

return object;
}

var object1 = createObject();
var object2 = createObject();

object1.get();
</script>
</head>

<body>

</body>
</html>

带参数的构造方法:

function  createObject(username,password)
{
var object = new Object();

object.username = username;
object.password = password;

object.get = function()
{
alert(this.username + "," + this.password);
}

return object;
}
var object1 = createObject("zzzzzz","111");
object1.get();


上述方式中每个对象都都有一个get方法

以下是改进:

function get()
{
alert(this.username + "," + this.password);
}

function  createObject(username,password)
{
var object = new Object();

object.username = username;
object.password = password;

object.get = get;

return object;
}

var object1 = createObject("zhan","123");
var object2 = createObject("ddd","345");

object1.get();
object2.get();


多个对象共享一个get方法。

    3)构造函数方式:

<html>
<head>
<script type="text/javascript">
function Person()
{
//在执行第一行代码前,js引擎会为我们生成一个对象
this.username = "aaa";
this.password = "123";

this.getInfo = function()
{
alert(this.username + this.password);
}
//此处有一个隐藏的return语句,用于将之前生成的对象返回
}
var person = new Person();
person.getInfo();
</script>
</head>

<body>

</body>
</html>


带参数的构造函数方法:可以在构造对象时传递参数

<html>
<head>
<script type="text/javascript">
function Person(username,password)
{

this.username =username;
this.password = password;

this.getInfo = function()
{
alert(this.username + this.password);
}

}
var person = new Person("zhan","123");
person.getInfo();
</script>
</head>

<body>

</body>
</html>


    4)原型(“prototype”)方式

<html>
<head>
<script type="text/javascript">
//使用原型(prototype)方式创建对象
function Person()
{

}

Person.prototype.username = "zhangsan";
Person.prototype.password = "123";

Person.prototype.getInfo = function()
{
alert(this.username + this.password);
}

var person = new Person();
var person2 = new Person();
person.username = "lisi";
person.getInfo();//lisi,123
person2.getInfo();//zhangsan,123

</script>
</head>

<body>

</body>
</html>


单纯使用原型方式定义类无法在构造函数中为属性赋初值,只能在对象生成后再去改变属性值。

如果使用原型方式对象,那么生成的所有对象会共享原型中的属性,这样一个对象改变了该属性也会反映到其他对象当中:

<html>
<head>
<script type="text/javascript">
//使用原型(prototype)方式创建对象

function Person()
{

}

Person.prototype.username = new Array();
Person.prototype.password = "123";

Person.prototype.getInfo = function()
{
alert(this.username + this.password);
}

var person = new Person();
var person2 = new Person();

person.username.push("zhangsan");
person.username.push("lisi");
person.password = "456";

person.getInfo();//zhangsan,lisi,456
person2.getInfo();//zhangsan,lisi,123
</script>
</head>

<body>

</body>


注意person.getInfo()与person2.getInfo()的结果:至于原因,看以下的内存图:



    5)使用原型+构造函数方式来定义对象,对象之间的属性互不干扰,各个对象间共享同一个方法

<html>
<head>
<script type="text/javascript">
//使用原型+构造函数方式来定义对象

function Person()
{
this.username = new Array();
this.password = "123";
}

Person.prototype.getInfo = function()
{
alert(this.username + "," + this.password);
}

var p = new Person();
var p2 = new Person();

pn.username.push("zhangsan");
p2.username.push("lisi");

p.getInfo();//zhangsan,123
p2.getInfo();//lisi,123
</script>
</head>

<body>

</body>
</html>


    6)动态原型方式:在构造函数中通过标志量让所有对象共享一个方法,而每个对象拥有自己的属性

<html>
<head>
<script type="text/javascript">
//使用动态原型方式来定义对象

function Person()
{
this.username = "zhangsan";
this.password = "123";

if(typeof Person.flag == "undefined")
{
alert("invoke");
Person.prototype.getInfo = function()
{
alert(this.username + "," + this.password);
}

Person.flag = true;
}
}

var p = new Person();
var p2 = new Person();

p.getInfo();
p2.getInfo();
</script>
</head>

<body>

</body>
</html>


 4、Javascript中的继承

    1)对象冒充:

<html>
<head>
<script type="text/javascript">
//继承第一种方式:对象冒充

function Parent(username)
{
this.username = username;

this.sayHello = function()
{
alert(this.username);
}
}

function Child(username,password)
{
//下面三行代码是最关键的代码
this.method = Parent;
this.method(username);
delete this.method;

this.password = password;
this.sayWorld = function()
{
alert(this.password);
}

}

var parent = new Parent("zhang");
var child = new Child("lisi","123");
child.sayHello();
child.sayWorld();
</script>
</head>

<body>

</body>
</html>


    2)Call方法方式

call方法是Function对象中的方法,因此我们定义的每个函数都拥有该方法。可以通过函数名来调用call方法,call方法的第一个参数会被传递给函数中的this,从第二个参数开始,逐一赋值给函数中的参数。

<html>
<head>
<script type="text/javascript">
//继承第二种方式:Call方法方式,Function对象中的方法

function test(str)
{
alert(this.name + "," + str);

}
var object = new Object();
object.name = "zhang";
//test.call相当于调用了test函数
test.call(object,"lisi"); //将object赋给this了

</script>
</head>

<body>

</body>
</html>

    使用call实现对象的继承:

<html>
<head>
<script type="text/javascript">
//使用Call方式实现对象的继承
function Parent(username)
{
this.username = username;
this.sayHello = function()
{
alert(this.username );
}
}

function Child(username,password)
{
Parent.call(this,username);
this.password = password;

this.sayWorld = function()
{
alert(this.password);
}
}

var parent = new Parent("zhang");
var child = new Child("lisi","123");

parent.sayHello();
child.sayHello();
child.sayWorld();


    3)apply方法方式继承对象

<html>
<head>
<script type="text/javascript">
//使用apply方式实现对象的继承
function Parent(username)
{
this.username = username;
this.sayHello = function()
{
alert(this.username );
}
}

function Child(username,password)
{
Parent.apply(this,new Array(username));//与call的不同
this.password = password;

this.sayWorld = function()
{
alert(this.password);
}
}

var parent = new Parent("zhang");
var child = new Child("lisi","123");

parent.sayHello();
child.sayHello();
child.sayWorld();

</script>
</head>

<body>

</body>
</html>


    4)原型链方式实现对象继承(缺陷:无法给构造函数传递参数)

<html>
<head>
<script type="text/javascript">
//使用原型链(prototype chain)方式实现对象的继承
function Parent()
{

}
Parent.prototype.hello = "hello";

Parent.prototype.sayHello = function()
{
alert(this.hello);
}

function Child()
{

}

Child.prototype = new Parent();

Child.prototype.world = "world";

Child.prototype.sayWorld = function()
{
alert(this.world);
}

var child = new Child();

child.sayHello();
child.sayWorld();

</script>
</head>

<body>

</body>
</html>


    5)混合方式(对原型链的改进)(推荐)

<html>
<head>
<script type="text/javascript">
//混合方式实现对象的继承(推荐)
function Parent(hello)
{
this.hello = hello;
}

Parent.prototype.sayHello = function()
{
alert(this.hello);
}

function Child(hello,world)
{
Parent.call(this.hello);
this.world = world;
}

Child.prototype = new Parent();

Child.prototype.sayWorld = function()
{
alert(this.world);
}

var child = new Child("hello","world");

child.sayHello();
child.sayWorld();

</script>
</head>

<body>

</body>
</html>


5、定义一个Shape对象,扩展出三角形和四边形

<html>
<head>
<script type="text/javascript">
function Shape(edge)
{
this.edge = edge;
}

Shape.prototype.getArea = function()
{
return -1;
}

Shape.prototype.getEdge = function()
{
return this.edge;
}
function Triangle(bottom,height)
{
Shape.call(this,3);
this.bottom = bottom;
this.height = height;
}

Triangle.prototype = new Shape();
Triangle.prototype.getArea = function()
{
return 0.5 * this.bottom * this.height;
}
//Triangle.prototype.getEdge = function()
//{
//	return this.edge;
//}

var triangle = new Triangle(10,4);
alert(triangle.getEdge() + "=" + triangle.getArea());

function Rectangle(bottom,height)
{
Shape.call(this,4);
this.bottom = bottom;
this.height = height;
}
Rectangle.prototype = new Shape();
Rectangle.prototype.getArea = function()
{
return this.bottom * this.height;
}

//Rectangle.prototype.getEdge = function()
//{
//	return this.edge;
//}

var rectangle = new Rectangle(10,20);
alert(rectangle.getEdge() + "=" + rectangle.getArea());
</script>
</head>

<body>

</body>
</html>


6、firefox的debug功能

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: