【JS】面向对象-构造函数使用注意事项

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script>
  //1.构造函数与普通函数没有区别
  //=>只不过在调用的时候需要与new 连用
  //2.书写构造函数,函数名首字母大写
  //3.调用时必须要与new连用,否则没有创建和返回的能力
  //4.调用构造函数的时候,如果不需要传递参数,可以不写最后的小括号(但是不推荐)
  //5.构造函数内不要写return
  function person() {
    console.log("Hello")
  }
  function Person() {
      this.name='Jack'
      this.age=23
  }
  var obj=new Person()//推荐
  console.log(obj)
  var obj2=Person()
  console.log(obj2)
  var obj3=new Person//不推荐
  console.log(obj3)
</script>
</body>
</html>

 

posted @ 2022-07-20 11:21  木子欢儿  阅读(23)  评论(0编辑  收藏  举报