获取具有4个属性的JavaScript对象:
function Object() {
this.prop1;
this.prop2;
this.prop3;
this.prop4;
}
var obj = new Object();
Run Code Online (Sandbox Code Playgroud)
我使用for(in)循环来检查每个属性,因为我不知道属性的数量或名称:
for(property in obj) {
var prop = obj[property];
}
Run Code Online (Sandbox Code Playgroud)
但是我想处理从最后一个开始的属性(本例中为prop4).我想我想要一个反向循环.
我怎样才能做到这一点?
谢谢,杰克
添加:我引用的对象是从JSON.parse返回的对象.这些属性似乎始终如一.没有keys()方法.
我想编写一个 Chrome 扩展程序,它利用内容脚本将一些 JavaScript 注入特定网页,使用 CSS 选择器抓取一些数据,最后将数据存储在 Google 表格中。
我编写了一个 Chrome 扩展程序(内容脚本)来抓取 HTML 页面,我不清楚的地方是如何最好地将数据插入到 Google 表格中。
我开始阅读这个主题,很快就被可用的选项所淹没,需要一些澄清和指导。
我已经确定了三个选项。在最高级别上,它们是:
我赞成选项 1,因为它是独立的。我的问题是:
google-chrome-extension google-spreadsheet-api google-drive-api
使用Material Design for Angular 时,如何在Select 组件上设置默认(即选定)选项?
<md-select placeholder="Angular2 Material">
<md-option>One</md-option>
<md-option selected>Two</md-option>
<md-option ng-selected="true">Three</md-option>
</md-select>
Run Code Online (Sandbox Code Playgroud)
搜索 SO 引导我:
我已经尝试过ng-selected="true"和selected属性,都没有工作。
Plunker:https ://plnkr.co/edit/EyC6wpUpgZEihlGclDUU ? p = preview
需要明确的是,我没有使用Material Design for AngularJS Apps。我正在为 Angular使用Material Design。
我正在使用rules_protobuf为我的helloworld.proto文件构建 Python 语言绑定。我的helloworld.proto进口wrappers.proto。
syntax = "proto3";
package main;
import "google/protobuf/wrappers.proto";
Run Code Online (Sandbox Code Playgroud)
我的构建文件
load("@org_pubref_rules_protobuf//python:rules.bzl", "py_proto_compile")
# Wrapper around proto_compile.
# https://github.com/pubref/rules_protobuf/blob/master/protobuf/internal/proto_compile.bzl
py_proto_compile(
name = "py",
with_grpc = True,
protos = ["helloworld.proto"],
imports = ["/usr/local/home/username/myproject/include"]
)
Run Code Online (Sandbox Code Playgroud)
该wrappers.proto文件位于目录中
/usr/local/home/username/myproject/include
Run Code Online (Sandbox Code Playgroud)
Bazel 规则py_proto_compile由rules_protobuf定义并记录在 README.md 中。imports定义为:
imports[]我的 BUILD 规则有效,但是我已经硬编码了wrappers.protowith的位置:
imports = ["/usr/local/home/username/myproject/include"]
Bazel 似乎没有任何引用我的 WORKSPACE …
这与如何在 Angular 中初始化 Firebase 应用程序检查不重复,因为我可以使用 reCAPTCHA Enterprise 成功初始化应用程序。这个问题是关于使用调试提供程序初始化应用程序。
Firebase文档建议在从本地主机进行测试时使用调试提供程序。具体来说
self.FIREBASE_APPCHECK_DEBUG_TOKEN在调试版本中,在初始化 App Check 之前设置为 true 来启用调试模式 。例如:
self.FIREBASE_APPCHECK_DEBUG_TOKEN = true;
initializeAppCheck(app, { /* App Check options */ });
Run Code Online (Sandbox Code Playgroud)
如何在 Angular 应用程序中实现这一点?目前尚不清楚self引用了什么或该代码片段应放置在何处。
采用以下模型:
MyModel= Backbone.Model.extend({
defaults : {
myNestedModel:undefined,
},
initialize: function() {
this.set({myNestedModel: new MyNestedModel());
}
});
Run Code Online (Sandbox Code Playgroud)
它有一个名为'myNestedModel'的属性,它具有以下定义:
MyNestedModel= Backbone.Model.extend({
defaults : {
myModel:undefined,
}
});
Run Code Online (Sandbox Code Playgroud)
它也有一个属性名称'myModel'.现在,如果我创建MyModel的实例:
aModel = new MyModel();
嵌套模型将在MyModel的initialize方法中设置.然后我在两个步骤中使用JSON.stringify:
// Use Backbone.js framework to get an object that we can use JSON.stringfy on
var modelAsJson = aModel.toJSON();
// Now actually do stringify
var modelAsJsonString = JSON.stringify(modelAsJson);
Run Code Online (Sandbox Code Playgroud)
这工作正常,我得到了MyModel的JSON表示,它的属性是MyNestedModel.我使用默认值时会出现问题,例如:
MyModel= Backbone.Model.extend({
defaults : {
new MyNestedModel(),
}
});
Run Code Online (Sandbox Code Playgroud)
这会导致JSON.stringify出现问题,因为它不支持循环引用.我假设正在创建循环引用,因为MyModel的所有实例共享MyNestedModel的相同实例.而initialize方法为每个实例创建一个新的嵌套模型.
defaults:{}这个问题的"原因"的理解是否正确?defaults:{}
关于值何时应用,何时被覆盖以及实例是否共享相同的默认"实例" 的用法?我想将来自backbone.js Model对象的所有属性复制到另一个属性(如果它们未定义).这有点像一个覆盖父母属性的孩子.子对象的某些属性应该从整个过程中排除.这是我到目前为止:
function inherit(parent, child) {
var childAttributes = Object.keys(child.attributes);
// Don't involve these attributes in any way, they're special.
attributes = _.without(attributes, ['specialCase1', 'specialCase2', 'specialCase3']);
// This array will store undefined child attributes
var undefChildAttributes = new Array();
for (i in attributes) {
var attr = attributes[i]; // Get attribute name
var value = child.get(attr); // ... and value
var type = RealTypeOf(value); // ... and type
if (type == 'undefined') {
undefChildAttributes.push(attr);
}
}
// Update the child …Run Code Online (Sandbox Code Playgroud) 鉴于一个模型:
MyModel = Backbone.Model.extend({
defaults: {
name: '',
age: -1,
height: '',
description: ''
}
});
Run Code Online (Sandbox Code Playgroud)
和一个视图将模型呈现为列表:
MyView = Backbone.View.extend({
tagName: 'ul',
className: 'MyView',
render() {
var values = {
name: this.model.get('name'),
age: this.model.get('age'),
height: this.model.get('height'),
description: this.model.get('description')
}
var myTemplate = $('#MyView-Template').html();
var templateWithValues = _.template(myTemplate , values);
}
});
Run Code Online (Sandbox Code Playgroud)
以及View加载的模板:
<script type="text/template" id="MyView-Template">
<li class="name"><%= name %></li>
<li class="age"><%= age %></li>
<li class="name"><%= height%></li>
<li class="name"><%= description%></li>
</script>
Run Code Online (Sandbox Code Playgroud)
一切正常,虽然这是一个人为的例子,真正的代码在模型中有很多很多属性.我遇到的问题是如何处理模型的更新.
我创建了一个HTML表单,其中包含每个字段的相应输入元素.表单被建模并作为模板加载:
<script type="text/template" id="MyEditView-Template">
<input type"text" value="<%= …Run Code Online (Sandbox Code Playgroud) bytearray类型是0 <= x <256范围内的可变整数序列.
但是,以下代码建议值可以> = 256.我存储一个9位二进制数,其最大值为: 2^9-1 = 512-1 = 511
ba = bytes([0b111111111])
print '%s' % (ba)
Run Code Online (Sandbox Code Playgroud)
9位二进制数打印为十进制511:
[511]
Run Code Online (Sandbox Code Playgroud)
我不知道预期的行为是什么,但我认为最重要的位将被丢弃以给出8位数.
我想使用类型开关来调用特定于类型的解析函数
https://play.golang.org/p/2xj_owLL4ZK
package main
import (
"fmt"
)
func main() {
var value interface{}
value = "I am a string"
switch v := value.(type) {
case string:
parseString(value)
default:
fmt.Printf("I don't know about type %T!\n", v)
}
}
func parseString(s string) {
fmt.Println(s)
}
Run Code Online (Sandbox Code Playgroud)
但是,这不会编译,因为它缺少类型断言:
cannot use value (type interface {}) as type string in argument to parseString: need type assertion
添加类型断言可以修复该错误。
https://play.golang.org/p/p0nYNEEJb0Z
package main
import (
"fmt"
)
func main() {
var value interface{}
value …Run Code Online (Sandbox Code Playgroud) backbone.js ×3
javascript ×2
angular ×1
bazel ×1
binary ×1
byte ×1
firebase ×1
for-loop ×1
go ×1
python ×1