创建自定义函数

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
var xiao = {}; //定义空对象
var xiao1 = {name:'明明'}; //定义空对象,并且給对象中添加name属性
var xiao3 = {name:'明明',age:'18',sex:'男'} //定义含有3个属性值的对象
</script>
<script type="text/javascript">
var x = {
name = '明明',
age = 18,
sex = '男',
sayHello:function(){
console.log(x.constructor);
  }
}
</script>
<!--创建自定义函数-->
<script type="text/javascript">
function x (name,age){
this.name = name;
this.age = age;
this.sayHello = function(){
alert('你好'+this.name);
}
}
var p1 = new x('小敏',18);
var p2 = new x('小南',17);
alert(p1);
alert(p2);
p1.sayHello();
p2.sayHello();
document.write(p2.constructor)
</script>
</body>
</html>

posted @ 2020-04-09 09:39  妹妹早上好  阅读(175)  评论(0编辑  收藏  举报