我喜欢Object.getOwnPropertyNames方法.它似乎是一个从JS shell中学习对象的有用工具.
然而,令我疯狂的是,getOwnPropertyNames似乎缺少一些(注意:在我的测试中,我运行的是ECMA 5实现 - 谷歌Chrome版本28.0.1500.95).
这是一个例子:
> var x= []
undefined
> x.constructor
function Array() { [native code] }
> Object.getOwnPropertyNames(x)
["length"]
Run Code Online (Sandbox Code Playgroud)
很明显,x有很多属性!(例如https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype)
> x.push
function push() { [native code] }
> x.pop
function pop() { [native code] }
Run Code Online (Sandbox Code Playgroud)
谁能帮忙解释一下这里发生了什么?谢谢!:d
编辑:好的!我看到getOwnPropertyNames只获取手头对象的属性名称.有没有一种简单的方法来获得继承属性?或者也许唯一的方法是遍历object.constructor.prototype .__ proto__?
是否可以在JavaScript对象中使用setTimout()?
目前动画方法调用运行一次,似乎setTimeout()没有完成它的工作.我已经设法让它工作,但是在一个非常hackish方法中,在类之外使用setTimeout.我想让动画循环成为AnimationManager类的工作.如果你能看到任何不良做法,或者我出错了......请给我一个抬头!
JavaScript的:
var AnimationManager = function(canvas)
{
this.canvas = canvas;
this.canvasWidth = canvas.width();
this.canvasHeight = canvas.height();
this.ctx = canvas.get(0).getContext('2d');
this.running = true;
this.start = function start(){
this.running = true;
this.animate();
}
/** Allow the animations to run */
this.run = function run(){
this.running = false;
}
/** Stop the animations from running */
this.stop = function stop(){
this.running = false;
}
this.animate = function animate()
{
if(this.running)
{
this.update();
this.clear();
this.draw();
}
setTimeout(this.animate, 40); //25 fps
}
/** Update …Run Code Online (Sandbox Code Playgroud) 我创建了一个自定义对象,并向其中添加了一个名为“changeColor”的方法,该方法带有一个属性。这个想法是,当我将此方法附加到任何 DOM 元素时,元素的所有内容颜色都必须更改。
这是我开始的代码:
function CustomColor() {
}
CustomColor.prototype.changeColor = function(color){
//here what I have to specify.
}
Run Code Online (Sandbox Code Playgroud)
这是非常基础的,但我对 JavaScript 很陌生。谢谢。
我有一个非常大的Javascript对象,其中一个空白部分,我可以动态地向其添加数据.出于这个问题的目的,我删除了对象的不必要部分.
这是我的目标:
var simplemaps_worldmap_mapdata = {
locations:{
}
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试将数据插入对象:
var mainObj = simplemaps_worldmap_mapdata;
var newObj = [];
newObj.push({
name: 'newName',
lat: 'newLat',
lng: 'newLong',
color: 'newColor',
description: 'newDesc',
url: 'newUrl',
size: 'newSize',
type: 'newType',
opacity: 'newOpacity'
});
mainObj.locations.push(newObj);
Run Code Online (Sandbox Code Playgroud)
为什么我不能动态地向我的对象添加数据?
编辑:
这是一个如何locations看待一个条目的示例:
locations:{
0: {
name: 'newName',
lat: 'newLat',
lng: 'newLong',
color: 'newColor',
description: 'newDesc',
url: 'newUrl',
size: 'newSize',
type: 'newType',
opacity: 'newOpacity'
},
},
Run Code Online (Sandbox Code Playgroud) 在这个stackoverflow线程中,我了解到你可以通过一个简单的字符串获得一个对象路径.
考虑以下:
var person = { name: "somename", personal: { weight: "150", color: "dark" }};
var personWeight = deep_value(person,"personal.weight");
Run Code Online (Sandbox Code Playgroud)
我试图从我的'person'对象构造一个不属于'object'类型的对象值数组.
因此数组看起来像:
[['name', []],['personal.weight', []],['personal.color', []]];
Run Code Online (Sandbox Code Playgroud)
我希望他们以这种形式看待,因为我将继续使用它.
这就是我尝试过的:
var toIterate = { name: "somename", personal: { age: "19", color: "dark" } }
var myArray = [];
$.each(toIterate, recursive);
function recursive(key, value) {
if (key !== null) {
myArray.push([key, []]);
}
else {
$.each(value, recursive);
}
}
console.log(myArray);
Run Code Online (Sandbox Code Playgroud) 如果我有一个像这样的对象数组,我一直试图解决这个问题很长一段时间:
var my_array = [
Object {Project: A, Hours: 2},
Object {Project: B, Hours: 3},
Object {Project: C, Hours: 5},
Object {Project: A, Hours: 6},
Object {Project: C, Hours: 9}
]
Run Code Online (Sandbox Code Playgroud)
我想将具有相同键的所有对象合并到一个对象中,以便将它们的小时数相加:
预期产量:
my_array = [
Object {Project: A, Hours: 8}
Object {Project: B, Hours: 3}
Object {Project: C, Hours: 14}
]
Run Code Online (Sandbox Code Playgroud)
我该如何处理这个问题?我花了很长时间才能以这种方式格式化我的数据,这是最后一步!
我的尝试,我得到我循环数组,不知道如何处理对象的合并:
for (var i =0; i<my_array.length; i++) {
my_array[i].Project // access the object project key
my_array[i].Hours // need to increment hours
}
Run Code Online (Sandbox Code Playgroud) 以下是来自http://gruntjs.com/getting-started的示例gruntjs
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
build: {
src: 'src/<%= pkg.name %>.js',
dest: 'build/<%= pkg.name %>.min.js'
}
}
});
// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-uglify');
// Default task(s).
grunt.registerTask('default', ['uglify']);
};
Run Code Online (Sandbox Code Playgroud)
然后提到:
由于<%%>模板字符串可能引用任何配置属性,因此可以通过这种方式指定配置数据(如文件路径和文件列表)以减少重复.
我的问题:
这<%= %>是什么意思?它是gruntjs语法还是在其他地方普遍使用?我在哪里可以找到它的定义?
您搜索神秘符号解释的一般方法是什么?如果我在google/stackoverflow中搜索这些字符串(" <%="," <%",包括引用与否),基本上不会出现合理的结果.
我有一个JavaScript对象.
var obj = { Id: "100", Name: "John", Address: {Id:1,Name:"Bangalore"} }
var dataToRetrieve= "Name";
function GetPropertyValue(object,dataToRetrieve){
return obj[dataToRetrieve]
}
var retval = GetPropertyValue(obj,dataToRetrieve)
Run Code Online (Sandbox Code Playgroud)
这很好用.但是,如果我尝试获取"Address.Name"的属性值的值,
喜欢:var dataToRetrieve = "Address.Name";
它表明undefined.
注意:属性变量由用户从HTML设置并且可以根据用户要求(他想要的属性值)进行更改.
我想要实现的目标:
1)如果dataToRetrieve = "Name",它应该给我"John",
2)如果dataToRetrieve = "Id",它应该给我"100",
3)如果dataToRetrieve = "Address.Name",它应该给我"Bangalore",
4)如果dataToRetrieve = "Address.Id",它应该给我1
Plunkr在这里:PLUNKR
我想要一个 javascript 中的方法,它获取一个字符串作为其参数,并从嵌套对象返回一个值,如下所示:
var obj = {
place: {
cuntry: 'Iran',
city: 'Tehran',
block: 68,
info: {
name :'Saeid',
age: 22
}
}
};
function getValue(st) {
// st: 'place[info][name]'
return obj['place']['info']['name'] // or obj.place.info.name
}Run Code Online (Sandbox Code Playgroud)
我正在使用ReactJS访问API。在React组件访问API提供的对象中可能是“未定义”的属性时,阻止React组件崩溃的最佳方法是什么?
一个错误的例子是:
TypeError:无法读取未定义的属性“ items”
javascript ×10
arrays ×2
jquery ×2
dom-events ×1
gruntjs ×1
oop ×1
prototype ×1
reactjs ×1
settimeout ×1