首页 > 其他分享 >前端歌谣-第六课-关于this指向

前端歌谣-第六课-关于this指向

时间:2023-09-09 22:00:57浏览次数:30  
标签:function console 歌谣 第六课 前端 var test obj log

前言

我是歌谣 最好的种树是十年前 其次是现在 今天继续给大家带来的是this的讲解

环境配置

npm init -y
yarn add vite -D

修改page.json配置端口

{
  "name": "demo1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "vite --port 3002"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "vite": "^4.4.9"
  }
}

基本案例

console.log(this==window)
var a=1;
var b=function(){
    return 'function'
}
console.log(a)
console.log(b)

运行结果

前端歌谣-第六课-关于this指向_环境配置

类与函数

class Test1{
    constructor(){

    }
  say(){

  }
  static do(){
    
  }
}
const test=new test1()



const Test=(function(){
    function Test2(){

    }
    Test2.prototype.say=function(){

    }
    Test.do=function(){

    }
    window.Test=Test
})

const test2=new Test2()

类组件案例

class Test{
    constructor(){
        this.test=function(){
            console.log("none-static",this)
        }
    }
   
    test(){
        console.log("static"+this)
    }
}
const test=new Test()
test.test()

const TestA=Object.create(null)
console.log(TestA)
const TestB={}
console.log(TestB)

运行结果

前端歌谣-第六课-关于this指向_Test_02

继承

class Father{
    constructor(){
        this.age=18
    }
    swim(){
        console.log("Go Swimming")
    }
}

class Son extends Father{
    constructor(){
         super()
        this.hobby="playing"
    }
    study(){
        console.log(this)
        this.swim()
    }
}
const son=new Son()
son.study()

运行结果

前端歌谣-第六课-关于this指向_ci_03

call,apply,bind

var obj={
    a:1
}
var obj2={
    a:100
}
var a=2;

function test(b,c){

    console.log(this.a,b,c)
}
test()
test.call(obj)
test.apply(obj)
test.call(obj,3,4)
test.apply(obj,[3,4])

var test1=test.bind(obj,3,4)
test1();

var test2=test.bind(obj2,3,4)
test2();

运行结果

前端歌谣-第六课-关于this指向_Test_04

严格模式

'use strict'
const test=()=>{
    console.log(this)
}
function test1(){
    console.log(this)
}
const test2=function(){
    console.log(this)
}
test()
test1()
test2()

运行结果

前端歌谣-第六课-关于this指向_环境配置_05

案例

var obj={
    a:1
}
var a=2

const test=()=>{
    console.log(this.a)
}

test()
test.call(obj)
test.apply(obj)
var test1=test.bind(obj);
test1()

运行结果

前端歌谣-第六课-关于this指向_Test_06

箭头函数和普通函数

var obj={}
var obj1={}
var obj2={}
obj.test = () => {
    console.log(obj)
    console.log(this)

}
obj.test()


obj1.test=function(){
    var t=()=>{
        console.log(this)
    }
    t()
}
obj1.test()

obj2.test=function(){
    console.log("test",this)
    var t1=function(){
        console.log("t1",this)
        var t2=()=>{
            console.log("t2",this)
        }
        t2()
    }
    t1()
}
obj2.test()

运行结果

前端歌谣-第六课-关于this指向_Test_07

案例

var obj={
    a:1,
    b:2,
    c:{
        d:4,
        test3:function(){
          
            console.log(this.d,"d is")
        }
    },
    test:function(){
        console.log(this.a,"a is")
    },
    test2:test2
}
function test2(){
    console.log(this.b,"a is")
}
obj.test()
obj.test2()
obj.c.test3()

运行结果

前端歌谣-第六课-关于this指向_环境配置_08

addEventListener案例

// var OBtn=document.getElementById("J_BUTTON")

// OBtn.onclick=function(){
//     console.log(this)
// }
// OBtn.addEventListener("click",function(){
//     console.log(this)
// },false)


; (function (doc) {
    var OBtn = document.getElementById("J_BUTTON")
    function Plus(a, b) {
        this.a = a;
        this.b = b
        this.init()
    }
    Plus.prototype.init = function () {
        this.bindEvent()
    }
    Plus.prototype.bindEvent = function () {
        OBtn.addEventListener("click",this.handleBtnClick.bind(this),false)
    }
    Plus.prototype.handleBtnClick = function () {
        console.log(this.a,this.b)
        console.log(this)
    }
    window.Plus=Plus
})(document)

new Plus(3,4)

运行结果

前端歌谣-第六课-关于this指向_Test_09

father和son

class Father{
    constructor(){
        this.eat=this.eat.bind(this)
    }
    get fruit(){
        return 'apple'
    }
    eat(){
        console.log('i an eat'+this.fruit)
    }
}

class Son{
    get fruit(){
        return 'orange'
    }
}
const father=new Father();
const son=new Son();
father.eat()
son.eat=father.eat
son.eat()

运行结果

前端歌谣-第六课-关于this指向_环境配置_10

总结

我是歌谣 最好的种树是十年前 其次是现在 微信公众号关注前端小歌谣一起学习前端知识

标签:function,console,歌谣,第六课,前端,var,test,obj,log
From: https://blog.51cto.com/u_14476028/7421164

相关文章

  • 一个由于前端缺少 encodeURIComponent 引起的登录问题的分析和解决
    笔者最近三年一直在SAP中国研究院担任Angular应用开发程序员的职位,负责的产品是SAP电商云SpartacusUI的开发。Spartacus是SAP公司主导的一个开源项目,Github项目地址:https://github.com/SAP/spartacus.电商云StorefrontUI界面如下,客户如果想在上面下单,需要点击Si......
  • 考试系统前端项目复盘
    前段时间和朋友做了一个局域网考试系统,总共有3个端:考生端、监考端、管理端。框架与相关的库先简单说明一下我使用的框架和相关的库:构建工具:Vite框架:Vue3UI组件库:element-plus网络请求库:axios路由跳转:vue-router状态管理:piniaCSS扩展语言:sass其它与项目......
  • 前端使用proxy代理解决跨域的问题
     实现方法:本地向proxy代理服务器发送请求,proxy接收本地请求,转换为目标地址相同IP和端口向目标地址发送请求。配置:(注意:因为我是本地启动的后端服务器,所以使用地址为localhost'一些为/api')vue.config.jsdevServer:{open:true,proxy:{"/localhost":{/......
  • web前端技能方法总结(css、js、jquery、html)(2)
    创建链接块display:block;列表样式在一个无序列表中,列表项的标志(marker)是出现在各列表项旁边的圆点。在有序列表中,标志可能是字母、数字或另外某种计数体系中的一个符号。要修改用于列表项的标志类型,可以使用属性list-style-type:ul{list-style-type:square;}1上面的声明把......
  • 前端几个常用的官网模版记录一下
    无意间发现的几个官网模版对于一些要求不高,不需要特定设计的官网,这几个模版套一套,改一改,轻松解决!......
  • 前端开发中如何高效渲染大数据量
    我们是袋鼠云数栈UED团队,致力于打造优秀的一站式数据中台产品。我们始终保持工匠精神,探索前端道路,为社区积累并传播经验价值。本文作者:琉易liuxianyu.cn  在日常工作中,较少的能遇到一次性往页面中插入大量数据的场景,数栈的离线开发(以下简称离线)产品中,就有类似的场景。......
  • 前端三件套案例
    以下是一个使用前端三件套(HTML、CSS、JavaScript)编写的最简单的前端案例:<!DOCTYPEhtml><html><head><title>最简单的前端案例</title><metacharset="UTF-8"><style>/*CSS样式*/h1{color:red;......
  • Axios 的 put 请求实践:实现前端与后端的数据同步
    在前端开发中,我们经常需要与后端服务器进行数据交互。其中,PUT 请求是一种常用的方法,用于向服务器发送更新或修改数据的请求。通过发送PUT请求,我们可以更新服务器上的资源状态。Axios是一个流行的JavaScript库,用于在浏览器和 Node.js 中进行 HTTP 请求。它提供了简单易用......
  • 使用EasyExcel下载文件时,前端获取不到后端返回的文件名,无法下载到本地
    【问题描述】使用EasyExcel下载文件时,前端获取不到后端返回的文件名,无法下载到本地 【原因分析】实际上文件的流后端已经返回了,只是缺少了Content-Disposition属性返回,前端无法获取到文件名;privatestaticOutputStreamgetOutputStream(StringfileName,HttpServletRespon......
  • web前端技能方法总结(css、js、jquery、html)
    CSS设置背景(background)背景颜色background-color背景图片background-image背景重复background-repeat:repeat-x/repeat-y背景位置background-position:bottom/left/top/right/center背景关联background-attachment:fixed/scroll综合写法:.tagName{background:#ffffffurl(“a......