浅析JavaScript中的常用算法与函数(2)


function Person1(name)
{
this.Name = name;
this.sayName = function()
{
alert(this.Name);
}
}
function Person2(sex)
{
this.Sex = sex;
this.saySex = function()
{
alert(this.sex);
}
}

function MyPerson(name,age,sex)
{
Person1.call(this,name);
Person2.call(this,sex);

this.Age = age;
this.sayAge = function()
{
alert(this.Age);
}

this.say = function()
{
alert(this.Name + "|" + this.Age + "|" + this.Sex);
}
}

var aMyPerson = new MyPerson("张三",25,"男");
aMyPerson.say();


0017:
继承的实现:原型链方式,不支持有参数的构造函数和多重继承

复制代码 代码如下:


function Person()
{

}

function MyPerson()
{

}
MyPerson.prototype = new Person();//不能有参数


0018:
合理的继承机制是混合使用以上的几种方式:

复制代码 代码如下:


function Person(name)
{
this.Name = name;
this.sayName = function()
{
alert(this.Name);
}
}

function MyPerson(name,age)
{
Person.call(this,name);//或者Person.apply(this,new Array(name));

this.Age = age;
this.sayAge = function()
{
alert(this.Age);
}

this.say = function()
{
alert(this.Name + "|" + this.Age);
}
}
MyPerson.prototype = new Person();

var aMyPerson = new MyPerson("张三",25);
aMyPerson.sayName();
aMyPerson.sayAge();
aMyPerson.say();


0019:
错误处理:

复制代码 代码如下:


<head>
<script type="text/Javascript">
window.onerror = function(msg,url,line)
{
var err = "错误信息:" + msg + "。/n" + "错误地址:" + url + "。/n" + "错误行数:" + line + "。/n";
alert(err);
}
</script>

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdwsjg.html