是否可以(以高性能的方式)在JavaScript中定义“后备”方法?
例如
function MyObject () {
/* what do i have to add here to have my defaultMethod? */
}
var obj = new MyObject ();
obj.doesntExistInMyObject (); // I want defaultMethod to be called
obj.doesntExistEither (); // I want defaultMethod to be called, too
Run Code Online (Sandbox Code Playgroud)
即:defaultMethod每当我编写obj.calledMethod ();和时obj.calledMethod == undefined,我都希望被调用,但是我不想undefined在调用代码中进行检查。
我希望获得页面上所有DOM元素的已使用css值.当我说"使用过的值"时,我指的是W3C规范中规定的定义:
6.1.3使用的值
在不格式化文档的情况下尽可能地处理计算值.但是,某些值只能在文档布局时确定.例如,如果元素的宽度设置为其包含块的某个百分比,则在确定包含块的宽度之前不能确定宽度.的使用的值是接受所计算的值,并解决任何剩余的依赖关系成绝对值的结果.
这些应该是相对于实际页面布局计算的最终值.Mozilla的文档声称可以window.getComputedStyle用来获取使用的值,但这对我来说没有意义,因为计算值与使用的值不同(我想要使用的值).即使这些是使用过的值,我也不确定这是否仅适用于Firefox.有没有办法在所有浏览器中可靠地获取使用过的值?
我在这里尝试反序列化这个JSON时遇到了麻烦:
{
"response": {
"numfound": 1,
"start": 0,
"docs": [
{
"enID": "9999",
"startDate": "2013-09-25",
"bName": "XXX",
"pName": "YYY",
"UName": [
"ZZZ"
],
"agent": [
"BobVilla"
]
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我为此创建的类是:
public class ResponseRoot {
public Response response;
}
public class Response {
public int numfound { get; set; }
public int start { get; set; }
public Docs[] docs;
}
public class Docs {
public string enID { get; set; }
public string startDate { get; set; } …Run Code Online (Sandbox Code Playgroud) 我有一个排序的静态列表要与KO一起显示,并且想在类别更改时显示类别标题(因为列表按类别排序)。我仍在了解KO,这是实现此目标的“ KO”方式,还是有更好的方法?特别是访问列表中上一个项目的语法有点冗长,这让我怀疑我缺少可以改善这一点的功能。:-)
HTML:
<table>
<tbody data-bind="foreach: items">
<!-- ko if: $index() === 0 || $parent.items()[$index() - 1].category() !== category() -->
<tr class="category">
<td colspan="2" data-bind="text: category"></td>
</tr>
<!-- /ko -->
<tr>
<td data-bind="text: item"></td>
<td class="num" data-bind="text: quantity"></td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
JavaScript :(显然,该示例只是一个快速而肮脏的VM)
function Item(category, item, quantity) {
this.category = ko.observable(category);
this.item = ko.observable(item);
this.quantity = ko.observable(quantity);
}
var vm = {
items: ko.observableArray([
new Item("Fruit", "Apples", 27),
new Item("Fruit", "Oranges", 17),
new Item("Fruit", "Kiwis", 3), …Run Code Online (Sandbox Code Playgroud) 我试图在使用Fetch API向服务器发出每个请求后运行一些简单的JS函数.我已经搜索了这个问题的答案,但是没有找到任何答案,可能是因为Fetch API是相对较新的.
我一直这样做XMLHttpRequest:
(function () {
var origOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function () {
this.addEventListener('load', function () {
someFunctionToDoSomething();
});
origOpen.apply(this, arguments);
};
})();
Run Code Online (Sandbox Code Playgroud)
很高兴知道是否有办法使用Fetch API完成同样的全局事务.
我在 JavaScript 中有一个构造函数,如下所示,并希望从数组中提供构造函数参数:
function dataRow(value1, value2, value3, value4, value5) {
this.val1 = value1;
this.val2 = value2;
this.val3= value3;
this.val4 = value4;
this.val5 = value5;
}
Run Code Online (Sandbox Code Playgroud)
现在我想要从数组提供的构造函数参数:
var dataArray = new Array();
dataArray("Value1","Value2","Value3","Value4","Value5","Value6"...........)
Run Code Online (Sandbox Code Playgroud)
我希望 (value1, value2, value3, value4, value5) 这部分从我的dataArray.
我正在开发一个 React+Redux 应用程序,该应用程序必须将某些值从商店保存到localStorage. 我有一些实用函数,可以将值安全地存储到localStorage. 我有一个动作(thunk)和减速器。
const wallet = (state = getFromLocalStorage('wallet'), action) => {
switch(action.type) {
case 'SET_WALLET':
setToLocalStorage('wallet', action.payload); // <--- **SIDE EFFECT?**
retrun action.payload;
default:
return state;
}
}
Run Code Online (Sandbox Code Playgroud)
减速器必须是纯函数。函数调用是否getToLocalStorage破坏了reducer的纯粹性?如果是这样,调用这个函数的正确位置在哪里?
我正在尝试创建一个HTMLHeadingElement. 为此,我使用:
const myHeading: HTMLHeadingElement = document.createElement("h3");
Run Code Online (Sandbox Code Playgroud)
所以这很完美。但是当我尝试更动态地创建这个元素时,我失败了:
const level: number = 3;
const headingLevel: string = "h" + level;
const myHeading: HTMLHeadingElement = document.createElement(headingLevel);
Run Code Online (Sandbox Code Playgroud)
所以,它看起来本质上是一样的,但显然不是。悬停时出现的错误myHeading是:
类型“HTMLElement”不可分配给类型“HTMLHeadingElement”
但是,在第一个示例中,该方法createElement("h3")立即返回 type HTMLHeadingElement。
现在,我的第一个问题是,这两个命令有何不同?如果不清楚,如何myHeading在第二个示例中获得与第一个示例中相同的结果?
React.createElement( 类型, [props], [...children] )
正如反应文档所说。但不知怎的,它对我来说不适用于多个道具。我尝试传递一个键值对象数组,传递一个带有键值的容器对象,但没有任何效果。让我展示一下我是如何尝试的:
//input.js
const Input = ({name, elementProps}) => {
const { element, props } = elementProps;
return React.createElement(element, props)
}
export default Input;
//props from
{
name: "msg",
element: "textarea",
props: [
{placeholder: "Message"},
{rows: "4"},
{cols: "50"},
]
}
//rendered by method
const { name, ...elementProps } = objectAbove;
return <Input name={name} elementProps={elementProps} />
Run Code Online (Sandbox Code Playgroud)
传递多个 props 所需的语法是什么?
我设法逐字节读取文本文件,并将字符集从 windows-1251 更改为 Unicode,以便通过input元素和FileReader. 现在我想对来自我的服务器的文件做同样的事情,通过fetch. 有可能这样做吗?(这fetch将在通过 HTTP 提供的页面中,并且 URL 将是相对的,例如fetch("raw/graph_tab.txt") - 我不会像使用input字段和那样尝试直接从最终用户的机器读取文件FileReader)。
工作代码块('windows-1251.js'库由 Mathias Bynens 创建):
<input type="file" id="file"/>
<script type="text/javascript">
document.getElementById("file").addEventListener("change", readFile, false);
function readFile (evt) {
var files = evt.target.files;
var file = files[0];
var reader = new FileReader();
reader.onload = function(event) {
console.log(windows1251.decode(event.target.result));
}};
Run Code Online (Sandbox Code Playgroud) javascript ×8
reactjs ×2
c# ×1
constructor ×1
css ×1
dom ×1
fetch ×1
fetch-api ×1
filereader ×1
function ×1
html ×1
json ×1
knockout.js ×1
redux ×1
typescript ×1
w3c ×1