liuzlzuil / blog Goto Github PK
View Code? Open in Web Editor NEW不定期整理更新文章,欢迎star。。。
不定期整理更新文章,欢迎star。。。
this
的几种指向this
指向全局对象console.log(this === window); // true
this
指向全局对象,严格模式下是undefined
function foo() {
console.log(this === window);
}
function bar() {
'use strict'; // 使用严格模式
console.log(this === undefined);
}
foo(); // true
bar(); // true
this
指向方法的拥有者var value = 'window';
var foo = {
value: 'foo',
getValue () {
console.log(this.value);
}
}
var getValue = foo.getValue;
foo.getValue(); // foo
getValue(); // window
new
生成的实例对象,this
指向实例对象本身function Foo(value) {
this.value = value;
}
Foo.prototype.getValue = function () {
console.log(this.value);
}
var foo = new Foo('实例foo');
foo.getValue(); // 实例foo
call,apply,bind
来改变this
的指向var value = 'window';
function fn() {
console.log(this.value);
}
var foo = {
value: 'foo'
}
fn(); // window
fn.call(foo); // foo
this
指向接收事件的元素<!-- html -->
<div id="foo"><div>
// js
var el = document.querySelector('#foo');
el.onclick = function() {
console.log(this.id);
}
el.click(); // foo
this
是根据外层 执行上下文决定的var value = 'window';
var foo = {
value: 'foo',
showValue: function () {
console.log(this.value);
}
}
var bar = {
value: 'bar',
showValue: () => {
console.log(this.value);
}
}
var baz = {
value: 'baz',
showValue: function () {
setTimeout(() => {
console.log(this.value);
}, 0);
}
}
foo.showValue(); // foo
bar.showValue(); // window 这里是指向外层所以不bar
baz.showValue(); // baz
new
出来的实例this
指向的是这个返回的对象function Foo(value) {
this.value = value;
return {
value: '返回一个对象',
getValue: function () {
console.log(this.value);
}
}
}
Foo.prototype.getValue = function () {
console.log(this.value);
}
function Bar(value) {
this.value = value;
return '返回一个字符串';
}
Bar.prototype.getValue = function () {
console.log(this.value);
}
var foo = new Foo('实例foo');
var bar = new Bar('实例bar');
foo.getValue(); // 返回一个对象
bar.getValue(); // 实例bar
this
不能被修改function fn() {
return () => {
console.log(this.value)
}
}
var foo = {
value: 'foo'
}
var bar = {
value: 'bar'
}
var arrowFn = fn.call(foo);
arrowFn(); // foo
arrowFn.call(bar); // foo
var value = 1;
function fn() {
console.log(this.value);
}
var foo = {
value: 2,
getValue: function(fn) {
this.value = 3;
fn(); // 第一行
arguments[0](); // 第二行
}
}
foo.getValue(fn);
欢迎star!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.