前端知识突击

3次阅读

共计 924 个字符,预计需要花费 3 分钟才能阅读完成。

JS 判断对象中是否有某属性

  • 通过. 或者[]
let test = {name: 'leemo'}

test.name 
test["name"]

test.age //undefined

可根据 Obj.x!== undefined 判断是否有该属性,但是不能判断属性存在且值等于 undefined 的情况

  • in

如果指定的属性存在于对象或原型链中,返回 true

'name' in test //true

无法分辨该属性存在于本身还是原型链上

  • hasOwnProperty()
test.hasOwnProperty('name')

只能判断自身属性


node.js 自己写服务器的方法

// 引入 http 模块
var http = require("http");
// 设置主机名
var hostName = '127.0.0.1';
// 设置端口
var port = 8080;
// 创建服务
var server = http.createServer(function(req,res){res.setHeader('Content-Type','text/plain');
    res.setHeader('Access-Control-Allow-Origin',"*")// 解决跨域
    res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); // 解决跨域
    res.end("hello world");
});
server.listen(port,hostName,function(){console.log(` 服务器运行在 http://${hostName}:${port}`);
});

在 html 代码中添加

  function getText(){$(".text").load("http:127.0.0.1:8080");
    }

即可用该页面请求我们写的 web 服务器
参考文档


React 兄弟组件间通信

原理:先把一个子组件数据传输到父组件,通过父组件传输到另外一个子组件,实现兄弟件组件通信

*待续


react 16.0 以后的生命周期函数

  • 钩子函数 componentDidCatch 如果 render()函数抛出错误,则会触发该函数
正文完
 0