试图绕过Javascript对OO的看法......和许多其他人一样,对constructor财产产生混淆.特别是constructor财产的重要性,因为我似乎无法使其产生任何影响.例如:
function Foo(age) {
this.age = age;
}
function Bar() {
Foo.call(this, 42);
this.name = "baz";
}
Bar.prototype = Object.create(Foo.prototype);
var b = new Bar;
alert(b.constructor); // "Foo". That's OK because we inherit `Foo`'s prototype.
alert(b.name); // "baz". Shows that Bar() was called as constructor.
alert(b.age); // "42", inherited from `Foo`.
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,对象b似乎有正确的构造函数call(Bar) - 并且它继承了age属性Foo.那么为什么许多人认为这是必要的步骤:
Bar.prototype.constructor = Bar;
Run Code Online (Sandbox Code Playgroud)
显然,Bar构造时会调用正确的构造函数b,因此这个原型属性有什么影响?我很想知道它实际上使构造函数属性设置'正确'有什么实际区别 - 因为我无法看到它对创建对象后实际调用的构造函数有任何影响.
对于英特尔架构,是否有一种方法可以指示GCC编译器生成的代码总是强制分支预测在我的代码中采用特定方式?英特尔硬件是否支持此功能?那么其他编译器或硬件呢?
我会在C++代码中使用它,我知道我希望快速运行的情况,并且不关心当另一个分支需要被采取时,即使它最近采用了该分支.
for (;;) {
if (normal) { // How to tell compiler to always branch predict true value?
doSomethingNormal();
} else {
exceptionalCase();
}
}
Run Code Online (Sandbox Code Playgroud)
作为Evdzhan Mustafa的后续问题,该提示是否可以在处理器第一次遇到指令时指定一个提示,所有后续的分支预测都能正常运行?
不确定这是否是特定于Mozilla的JS语法,但我经常发现变量是以这种方式声明的,例如,在附加SDK文档中:
var { Hotkey } = require("sdk/hotkeys");
Run Code Online (Sandbox Code Playgroud)
并在各种chrome Javascript(let声明被用来代替var),
let { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
Run Code Online (Sandbox Code Playgroud)
我发现它很混乱但我无法找到任何关于语法的文档,即使在MDN上也是如此.
所以我有一种情况,我有多个未知长度的承诺链.我希望在处理完所有CHAINS后运行一些操作.这甚至可能吗?这是一个例子:
app.controller('MainCtrl', function($scope, $q, $timeout) {
var one = $q.defer();
var two = $q.defer();
var three = $q.defer();
var all = $q.all([one.promise, two.promise, three.promise]);
all.then(allSuccess);
function success(data) {
console.log(data);
return data + "Chained";
}
function allSuccess(){
console.log("ALL PROMISES RESOLVED")
}
one.promise.then(success).then(success);
two.promise.then(success);
three.promise.then(success).then(success).then(success);
$timeout(function () {
one.resolve("one done");
}, Math.random() * 1000);
$timeout(function () {
two.resolve("two done");
}, Math.random() * 1000);
$timeout(function () {
three.resolve("three done");
}, Math.random() * 1000);
});
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我设置了一个$q.all()承诺一,二和三,这将在一些随机时间得到解决.然后我将承诺添加到第一和第三的末尾.我想要all解决所有链都已解决的问题.这是我运行此代码时的输出:
one done
one doneChained …Run Code Online (Sandbox Code Playgroud) 例如,如果我有两个对象:
var foo = {
x: "bar",
y: "baz"
}
Run Code Online (Sandbox Code Playgroud)
和
var oof = {}
Run Code Online (Sandbox Code Playgroud)
我想将x和y值从foo转移到oof.有没有办法使用es6解构语法?
也许是这样的:
oof{x,y} = foo
Run Code Online (Sandbox Code Playgroud) 好的,所以每个人都知道你可以用这个做三角形:
#triangle {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 100px solid red;
}
Run Code Online (Sandbox Code Playgroud)
这产生了一个坚实的,填充三角形.但是你会怎么做一个类似于空心的箭头状三角形呢?

我一直在看我正在处理的代码库中看到这样的函数:
const func = ({ param1, param2 }) => {
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
这究竟是做什么的?我很难在谷歌上找到它,因为我甚至不确定这是什么,或者如何在谷歌搜索中描述它.
我知道PHP中可能有"变量"变量.例如
$x = "variable";
$$x = "hello, world!";
echo $variable; // displays "hello, world!"
Run Code Online (Sandbox Code Playgroud)
是否可以在javascript中将变量的名称引用为字符串?怎么做?
get这个ES6课程意味着什么?我该如何参考这个功能?我该怎么用?
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
get area() {
return this.calcArea()
}
calcArea() {
return this.height * this.width;
}
}
Run Code Online (Sandbox Code Playgroud) javascript ×7
ecmascript-6 ×4
angularjs ×1
c++ ×1
constructor ×1
css ×1
css-shapes ×1
gcc ×1
getter ×1
html ×1
intel ×1
methods ×1
pragma ×1
promise ×1
prototype ×1
variables ×1
weakmap ×1