//---------------------------js代码---------------------------

function User(){
       //类成员的定义及构造函数
  this.name = "hello";
  this.age = 25;
  this.sayHello = function (){
    console.log("here is class1");
  }
}

//-------------------------html页面-------------------------

<script type="text/javascript" src="demo01.js"></script>
<script type="text/javascript">
var u = new User();
var say = new u.sayHello();
console.log(typeof(say))
</script>

 

 

 

总结:全局函数和作为一个对象方法定义的函数之间没有任何区别,因为可以把全局函数和变量看作为window对象的方法和属性。也可以使用new操作符来操作一个对象的方法来返回一个对象,这样一个对象的方法也就可以定义为类的形式,其中的this指针则会指向新创建的对象。

 

相关文章:

  • 2021-08-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-20
猜你喜欢
  • 2022-12-23
  • 2021-12-15
  • 2022-12-23
  • 2021-07-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案