我正在尝试编写一个接受字符串列表或单个字符串的函数.如果它是一个字符串,那么我想将它转换为只有一个项目的数组.然后我可以循环它而不用担心错误.
那么我该如何检查变量是否是一个数组?
我已经完成了下面的各种解决方案,并创建了一个jsperf测试.
newJavaScript中的关键字在第一次遇到时会非常混乱,因为人们倾向于认为JavaScript不是面向对象的编程语言.
如何检查值是否是JavaScript中的对象?
Javascript 1.9.3/ECMAScript 5介绍Object.create道格拉斯·克罗克福德等人长期以来一直在倡导.如何new在下面的代码中替换Object.create?
var UserA = function(nameParam) {
this.id = MY_GLOBAL.nextId();
this.name = nameParam;
}
UserA.prototype.sayHello = function() {
console.log('Hello '+ this.name);
}
var bob = new UserA('bob');
bob.sayHello();
Run Code Online (Sandbox Code Playgroud)
(假设存在MY_GLOBAL.nextId).
我能想到的最好的是:
var userB = {
init: function(nameParam) {
this.id = MY_GLOBAL.nextId();
this.name = nameParam;
},
sayHello: function() {
console.log('Hello '+ this.name);
}
};
var bob = Object.create(userB);
bob.init('Bob');
bob.sayHello();
Run Code Online (Sandbox Code Playgroud)
似乎没有任何优势,所以我想我没有得到它.我可能过于新古典主义了.我应该如何使用MY_GLOBAL.nextId创建用户'bob'?
这个基于构造函数的语法创建对象有什么区别:
person = new Object()
Run Code Online (Sandbox Code Playgroud)
...和这个文字语法:
person = {
property1 : "Hello"
};
Run Code Online (Sandbox Code Playgroud)
虽然JSLint更喜欢使用对象文字表示法,但它们似乎都做同样的事情.
哪一个更好,为什么?
在JavaScript中,这两个示例之间的区别是什么:
先决条件:
function SomeBaseClass(){
}
SomeBaseClass.prototype = {
doThis : function(){
},
doThat : function(){
}
}
Run Code Online (Sandbox Code Playgroud)
继承示例A使用Object.create:
function MyClass(){
}
MyClass.prototype = Object.create(SomeBaseClass.prototype);
Run Code Online (Sandbox Code Playgroud)
继承示例B使用new关键字
function MyClass(){
}
MyClass.prototype = new SomeBaseClass();
Run Code Online (Sandbox Code Playgroud)
这两个例子似乎做同样的事情.你什么时候选择一个而不是另一个?
另一个问题:考虑下面链接中的代码(第15行),其中对函数自身构造函数的引用存储在原型中.为什么这有用?
https://github.com/mrdoob/three.js/blob/master/src/loaders/ImageLoader.js
摘录(如果您不想打开链接):
THREE.ImageLoader.prototype = {
constructor: THREE.ImageLoader
}
Run Code Online (Sandbox Code Playgroud) 目前,我正在尝试async/await在类构造函数中使用.这样我就可以获得e-mail我正在研究的Electron项目的自定义标签.
customElements.define('e-mail', class extends HTMLElement {
async constructor() {
super()
let uid = this.getAttribute('data-uid')
let message = await grabUID(uid)
const shadowRoot = this.attachShadow({mode: 'open'})
shadowRoot.innerHTML = `
<div id="email">A random email message has appeared. ${message}</div>
`
}
})
Run Code Online (Sandbox Code Playgroud)
但是,目前该项目不起作用,出现以下错误:
Class constructor may not be an async method
Run Code Online (Sandbox Code Playgroud)
有没有办法绕过这个,以便我可以在其中使用async/await?而不是要求回调或.then()?
我试图在javascript中实现继承.我想出了以下最小代码来支持它.
function Base(){
this.call = function(handler, args){
handler.call(this, args);
}
}
Base.extend = function(child, parent){
parent.apply(child);
child.base = new parent;
child.base.child = child;
}
Run Code Online (Sandbox Code Playgroud)
专家,请告诉我这是否足够或我可能错过的任何其他重要问题.根据面临的类似问题,请提出其他更改建议.
这是完整的测试脚本:
function Base(){
this.call = function(handler, args){
handler.call(this, args);
}
this.superalert = function(){
alert('tst');
}
}
Base.extend = function(child, parent){
parent.apply(child);
child.base = new parent;
child.base.child = child;
}
function Child(){
Base.extend(this, Base);
this.width = 20;
this.height = 15;
this.a = ['s',''];
this.alert = function(){
alert(this.a.length);
alert(this.height);
}
}
function Child1(){
Base.extend(this, Child);
this.depth = …Run Code Online (Sandbox Code Playgroud) 我有一个基类:
function Monster() {
this.health = 100;
}
Monster.prototype.growl = function() {
console.log("Grr!");
}
Run Code Online (Sandbox Code Playgroud)
我想扩展并创建另一个类:
function Monkey extends Monster() {
this.bananaCount = 5;
}
Monkey.prototype.eatBanana {
this.bananaCount--;
this.health++; //Accessing variable from parent class monster
this.growl(); //Accessing function from parent class monster
}
Run Code Online (Sandbox Code Playgroud)
我做了很多研究,似乎有很多复杂的解决方案在JavaScript中这样做.在JS中实现这一目标的最简单,最可靠的方法是什么?
为什么这两个实现的行为不同?在评估他们的原型时,究竟是什么让他们与众不同?
使用指定的原型创建对象:
function Foo() {}
// creates an object with a specified prototype
var bar = Object.create(Foo);
console.log(Object.getPrototypeOf(bar)); // returns: function Foo(){}
console.log(Foo.isPrototypeOf(bar)); // returns: true
Run Code Online (Sandbox Code Playgroud)
使用构造方法创建对象:
function Foo() {}
// creates an object with the constructor method
var bar = new Foo();
console.log(Object.getPrototypeOf(bar)); // returns: Foo {}
console.log(Foo.isPrototypeOf(bar)); // returns: false
Run Code Online (Sandbox Code Playgroud)
另外,为什么第二个实现返回Foo {}和false?
javascript ×10
inheritance ×3
object ×3
new-operator ×2
arrays ×1
async-await ×1
constructor ×1
ecmascript-6 ×1
jslint ×1
node.js ×1
oop ×1
prototype ×1
types ×1