根据MDN,in操作员将返回true该属性是否存在,因此第一个示例记录为true。但是,当使用字符串文字时,为什么它会引发错误而不是记录为false?
let let1 = new String('test');
console.log(let1.length);
console.log('length' in let1)Run Code Online (Sandbox Code Playgroud)
var let1 = 'test';
console.log(let1.length);
console.log('length' in let1);Run Code Online (Sandbox Code Playgroud)
我使用console.log语句进行调试,但是遇到了一个使用','或'+'和console.log语句以不同模式记录输出的情况.例如
(function() {
var x = [];
x.push({
a: 1,
b: 2,
}, {
a: 4,
b: 3,
}, {
a: 5,
b: 6
}, {
a: 7,
b: 8,
})
console.log('Logging with , ', x);
console.log('Logging with + ' + x);
}())Run Code Online (Sandbox Code Playgroud)
当我使用','和console.log时,我看到输出为
Logging with , [Object, Object, Object, Object]
Run Code Online (Sandbox Code Playgroud)
并且每个这个对象都是可扩展的.但是用'+'我看到输出为
Logging with + [object Object],[object Object],[object Object],[object Object]
Run Code Online (Sandbox Code Playgroud)
为了演示,我创建了这个jsfiddle.
你能帮我理解为什么我们看到这种差异.
我有一个自定义插件,可以压缩我需要包含在我的 Maven 构建中的文件。所以我把这个插件包含在我的pom.xml:
<build>
// Other tags
<plugin>
<groupId>someGroupId</groupId>
<artifactId>somePlugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
由于它是一个自定义插件,因此在任何公共 Maven 存储库中都不可用。因此,每当我尝试构建时,都会收到一条错误消息:
Failed to read artifact .....
Run Code Online (Sandbox Code Playgroud)
即使我已将它添加到我的本地存储库。我如何引用我本地存储库中的这个插件?
我试图理解React.js并经常遇到一个术语"脏",如脏检查/检查,脏数据,脏模型
我跟着这个问题,但无法弄清楚实际上Dirty传达的术语以及为什么我们称之为脏.
我正在探索ES6 module,并试图找出什么额外的好处,我们开始使用ES6模块,而不是closure一起module pattern(MP).
例如ES6中的util.js.
var util ={
abc:function(){
//function body
},
def:function(){
// function body
}
export default utils; // here export is exposing the entire object
}
Run Code Online (Sandbox Code Playgroud)
util.js使用闭包和模块模式
var util = (function(){
function _abc(){
console.log("abc")
// function body
};
function _def(){
// function body
}
return{ // each of the function will be exposed
abc:_abc,
def:_def
}
}(util ||{}))
Run Code Online (Sandbox Code Playgroud)
ES6中的someFile.js
import {utils} from "path/to/file"
Run Code Online (Sandbox Code Playgroud)
在someFile.js中有闭包和模块模式
util.abc() // Will log "abc"
Run Code Online (Sandbox Code Playgroud)
我也知道ES6 module让我们重命名imports …
我知道这let将被提升到块的顶部,但是在初始化之前访问它将抛出ReferenceError到期Temporal Dead Zone
例如:
console.log(x); // Will throw Reference Error
let x = 'some value';
Run Code Online (Sandbox Code Playgroud)
但是像这样的代码片段会运行而不会出错:
foo(); // alerts foo;
function foo(){ // foo will be hoisted
alert("foo");
}
Run Code Online (Sandbox Code Playgroud)
我的问题
let当它在访问时会出现错误时,将其提升到顶部的目的是什么?也有var遭受TDZ的影响,我知道什么时候会抛出,undefined但是因为TDZ?
我是 angularjs 的新手,我遇到了这个 angular 服务组件 $window。在试图理解它时,我有这个片段
<div ng-app="miniapp" ng-controller="AppController">
</div>
Run Code Online (Sandbox Code Playgroud)
JS
var demoFunction =(function(){
var app = angular.module('miniapp', []);
app.controller("AppController",['$scope','$window',function($scope,$window){
alert($window.outerWidth);
}]);
}())
Run Code Online (Sandbox Code Playgroud)
我通过 css property 加载图像background,而不是使用src属性。但在这种情况下,alt文本也会显示出来。如何停止显示alt文本并仅在图像未加载时才显示
.test {
display: block;
width: 200px;
height: 82px;
background-size: 200px 82px;
background-image: url("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQJr81CRJeZGFiBsA9_AOyyxegiIPctdcbNfHThnpnclmFH-mJwoQ");
}Run Code Online (Sandbox Code Playgroud)
<img class="test" alt="My background image">Run Code Online (Sandbox Code Playgroud)
我正在尝试在 TypeScript 中使用 Fetch API,但由于某种原因我得到了
类型 'string' 不可分配给类型 'RequestMode'。
这是我的代码
export class MainService{
constructor(){}
request(requestObj){
if (!requestObj.url) {
return 'Please provide the end point url'
}
else {
let requestParam = {
method: requestObj.method ? requestObj.method : 'GET',
headers: new Headers(),
mode: 'cors'
};
fetch(endPointHost+requestObj.url,requestParam).then(function(resp){
//^^^ error thrown here
resp.json();
})
}
console.log(" Reached request");
}
}
Run Code Online (Sandbox Code Playgroud)
报告的错误是
TS2345:“
{ method: any; headers: Headers; mode: string; }”类型的参数不可分配给“RequestInit”类型的参数。属性“mode”的类型不兼容。类型“string”不可分配给类型“RequestMode”。
我正在尝试在方法链中创建一个方法 sleep(delay) 。对于这个我使用setTimeout带Promise。这将需要在 之后的任何方法sleep位于then.
现在我正在调用函数
lazyMan("John", console.log).eat("banana").sleep(5).then(d => {d.eat("apple");});.
这是我的代码
function lazyMan(name, logFn) {
logFn(name);
return {
eat: function(val) {
console.log(val);
return this;
},
sleep: function(timer) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(`Sleeping for ${timer} seconds`);
resolve(this);
}, timer * 1000);
}).then(d => this);
}
};
}
lazyMan("John", console.log)
.eat("banana")
.sleep(5)
.then(d => {
d.eat("apple");
});Run Code Online (Sandbox Code Playgroud)
有没有办法可以修改我的函数来调用它lazyMan("John", console.log).eat("banana").sleep(5).eat("apple")并以相同的顺序获取输出
我已经经历了在对象方法链中添加睡眠方法(JS)
javascript ×9
ecmascript-6 ×2
angularjs ×1
closures ×1
css ×1
html ×1
maven ×1
reactjs ×1
string ×1
typescript ×1