js学习笔记

来源:互联网 发布:intouch10.5软件下载 编辑:程序博客网 时间:2024/06/02 05:49
  1. js学习

    • 函数中this的意义
    //当函数作为对象的方法时,this代指这个对象var myObject = {    value: 0,    increament: function (inc) {        this.value += typeof inc == 'number' ? inc : 1;    }};// 当函数并非作为对象中的方法,他的this就指全局对象,但是可以通过以下方法使用myObject.double = function () {    var that = this;//这个this指myObject对象    var helper = function () {        that.value += 1;//如果用this 就是全局对象    }};//构造器函数:如果在一个函数前加上 new 来调用,js会创建一个连接到该函数的prototype成员的新对象,同时this会被绑定到那个新对象上var Quo = function (stirng) {    this.status = string;};var myQuo = new Quo("ok");//myQuo是连接到Quo.prototype的对象 构造函数中的this也指向这个新对象//在Quo.prototype中添加新方法,所有Quo.prototype的实例都可以获得这个方法Quo.prototype.getStatus = function () {    return this.status;}myQuo.getStatus();//ok//apply 调用模式//apply方法有两个参数,第一个是用来指定this,第二个是参数数组var statusObject = {    status: "not Ok"};//statusObject 并不是Quo的实例 //但是可以用apply调用Quo.prototype.getStatus();Quo.prototype.getStatus.apply(statusObject);// 返回值为 not Ok
0 0
原创粉丝点击