比方说,我们给了一个字符串,比如"itiswhatitis"一个子字符串"is".我需要在原始字符串中找到第二次出现'i'字符串的索引"is".
String.indexOf("is")在这种情况下将返回2.在这种情况下,我希望输出为10.
ES6引入了一种简写符号来初始化具有函数和属性的对象.
// ES6 shorthand notation
const obj1 = {
a(b) {
console.log("ES6: obj1");
}
};
// ES5
var obj2 = {
a: function a(b) {
console.log("ES5: obj2");
}
};
obj2.a();
obj1.a();
new obj2.a();
new obj1.a();Run Code Online (Sandbox Code Playgroud)
但是,正如您所看到的,这些不同的符号表现不同.如果我new obj1.a()在浏览器中(测试过的Chrome和Firefox),我会得到一个TypeError: obj1.a is not a constructor.new obj2.a()表现完全正常.
这里发生了什么?有没有人有解释,和/或文档/规范的链接?
Apple\xe2\x80\x99s 的解释是
\n\n\n\n\n\n
objc_getClass不同之处在于objc_lookUpClass,如果类未注册,objc_getClass则调用类处理程序回调,然后再次检查该类是否已注册。objc_lookUpClass不调用类处理程序回调。
但我不太理解“类处理程序回调”。你能更详细地解释一下吗?我希望一些代码可以显示它们的差异\xef\xbc\x8c非常感谢!
\n我了解 OOP 的主要原理,并且我有点知道如何将其实现到 JS 中。
function Person(name) {
this.name = name;
this.speak = function(msg) {
console.log('Person says:' + msg);
}
}
var dad = new Person('David');
dad.speak('I am your dad!');
Run Code Online (Sandbox Code Playgroud)
上面的脚本只是在控制台中打印出一条消息。我不明白我们如何用这种技术处理 DOM。也许是这样的?:
function Person(target, name) {
this.target = target;
this.name = name;
this.speak = function(msg) {
this.target.find('.speech-bubble').html(msg);
}
}
var dad = new Person($('#dad'), 'David');
dad.speak('I am your dad!');
Run Code Online (Sandbox Code Playgroud)
虽然这似乎不是一个好方法。
我们如何通过 OO Javascript 使用对象、方法、构造函数等操作 DOM?
我正在使用Aurelia.
我有一个post路线,显示一个给定通过的帖子id.服务器编译用于将帖子写入HTML的Markdown,我使用以下命令在我的模板中加载此内容:
<div innerHTML.bind="post.content"></div>
Run Code Online (Sandbox Code Playgroud)
// in the activate() function
client
.fetch(`api/post/${params.id}`)
.then(res => res.json())
.then(data => {
this.post = data;
// this is the workaround I am currently using
setTimeout(this.displayLineNumbers, 200);
});
Run Code Online (Sandbox Code Playgroud)
当内容附加到视图时,我找不到任何方法来执行函数.如果我只使用:
this.post = data;
this.displayLineNumbers();
Run Code Online (Sandbox Code Playgroud)
它将失败,因为内容尚未附加,因此我的功能要更改的元素尚不可用.我正在使用的当前解决方法是等待200ms然后执行该函数.
是否有任何方式(事件或功能)知道何时附加动态加载的内容?或者可能是另一种模仿内容的方式innerHTML.bind?
我正在使用Aurelia,我想用谷歌地图渲染一张基本地图。
我尝试了 Aurelia-Google-Maps ( https://github.com/Vheissu/aurelia-google-maps ),但无法使其正常工作(一切都正常加载,但<google-map>我模板上的元素未呈现为一张地图)。我现在正在尝试使用Google-Map-API,因此我可以正常加载所有内容,创建一个 Map 对象,但是当我尝试添加属性时,出现错误:
Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
Run Code Online (Sandbox Code Playgroud)
我根本不知道我的错误来自哪里。
这是我的代码(accueil.js——一个控制器)
import {inject} from 'aurelia-framework';
import mapsapi from 'google-maps-api';
@inject(mapsapi('myApiKey'))
export class Accueil {
constructor(mapsapi) {
mapsapi.then(function(maps) {
var map = new maps.Map((document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644}, // just random values
zoom: 8
})); // doesn't work
});
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢,
javascript ×4
aurelia ×2
dom ×1
ecmascript-6 ×1
google-maps ×1
indexof ×1
ios ×1
java ×1
jspm ×1
methods ×1
oop ×1
runtime ×1
shorthand ×1
string ×1
substring ×1