假设我创建一个对象如下:
var myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
Run Code Online (Sandbox Code Playgroud)
什么是去除财产的最好方式regex与新落得myObject如下?
var myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI"
};
Run Code Online (Sandbox Code Playgroud) 是否可以捕获对象的(任何)属性何时被访问或尝试访问?
例:
我创建了自定义对象 Foo
var Foo = (function(){
var self = {};
//... set a few properties
return self;
})();
Run Code Online (Sandbox Code Playgroud)
然后有一些行动反对Foo- 有人试图访问财产bar
Foo.bar
Run Code Online (Sandbox Code Playgroud)
是否有方法(原型,也许)捕获这个?bar可能未定义Foo.我可以捕获任何尝试访问未定义属性的功能.
例如,如果bar未定义Foo,并且Foo.bar尝试了,例如:
Foo.prototype.undefined = function(){
var name = this.name; //name of property they attempted to access (bar)
Foo[name] = function(){
//...do something
};
return Foo[name];
}
Run Code Online (Sandbox Code Playgroud)
但功能性,不像我的例子.
概念
Foo.* = function(){
}
Run Code Online (Sandbox Code Playgroud)
背景
如果我有自定义函数,每次调用此函数时都可以监听(见下文).只是想知道是否有可能获得财产.
Foo = function(){};
Foo.prototype.call = function(thisArg){
console.log(this, thisArg);
return this; …Run Code Online (Sandbox Code Playgroud) 我想构建一个检测对象更改的代理:
代码示例 1 -defineProperty
const me = {
name: "Matt"
}
const proxy = new Proxy(me, {
defineProperty: function(target, key, descriptor) {
console.log(`Property ${key} defined.`);
return Object.defineProperty(target, key, descriptor);
}
});
proxy // { name: 'Matt' }
proxy.name = "Mark";
// Property name defined.
// Mark
proxy.age = 20;
// Property age defined.
// 20
Run Code Online (Sandbox Code Playgroud)
代码示例 1 - 观察
proxy有一个属性name,这是我所期望的。name属性告诉我name已定义;不是我所期望的。age属性告诉我age已经定义;正如我所料。代码示例 2 - …
请考虑以下代码.第一个console.log正确记录图像,您可以在下面的图像中看到它的属性.但是,当我尝试将其属性记录到控制台时,我得到了undefined!
console.log(that.data[0].cards); //works -- see image below
console.log(that.data[0].cards.E); //undefined
console.log(that.data[0].cards['E']); //undefined
console.log(that.data[0].cards.hasOwnProperty('E')); //false
var test = JSON.stringify(that.data[0]);
console.log(test); // {}
for( var key in that.data[0].cards ) {
console.log('hello????') //doesn't appear in the console
}
console.log( Object.keys( that.data[0].cards ) ); //[]
console.log( that.data[0].cards.propertyIsEnumerable("E") ); //false
console.log( that.data[0].cards.__lookupGetter__( "E" ) ); //undefined
Run Code Online (Sandbox Code Playgroud)
控制台中的结果:

知道这里发生了什么吗?该xml内部的财产that.data[0]也应该有它的内部性能-命名一样,其实,在属性cards.
FWIW,我在Firebug中得到了相同的东西(上面的控制台图像是Chrome).
使用PowerShell,我试图理解默认排序属性的概念.
根据此示例,为Sort-Object命令提供:
PS C:\>get-childitem | sort-object
Run Code Online (Sandbox Code Playgroud)
因为没有属性指定,Sort-Object使用默认排序属性类型的对象file和directory,这是Name.
有没有办法知道,对于任何给定的类型,哪个是它的默认排序属性?
我正在使用 Jest 在 Node.js 应用程序上执行单元测试,其中代码源是用 TypeScript 编写的,然后编译为 JavaScript。
在我希望测试的一个类中,导入了一个外部模块并使用了该模块中的方法。我想模拟对此方法的调用,以便仅测试我的代码。
但是,当我运行测试时,出现以下错误:
TypeError: Cannot redefine property: methodName
问题是该方法具有以下对象属性:
{ value: [Function],
writable: false,
enumerable: true,
configurable: false }
Run Code Online (Sandbox Code Playgroud)
这configurable: false就是它成为一个大问题的原因。我无法在模拟调用之前重新定义属性以使其可写。
相关代码如下:
测试类
import externalType from 'external-module-name';
export class ClassName {
public propertyName: externalType;
public method(param: string): Promise<any> {
return new Promise((resolve, reject) => {
this.propertyName.externalMethod(param)
.then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
});
}
}
Run Code Online (Sandbox Code Playgroud)
单元测试
import { ClassName } from 'path/to/class';
describe('class', () => {
const …Run Code Online (Sandbox Code Playgroud) 我试图找出两个物体之间的差异
previousChart: {BWP: 1, ZAR: 1.3, USD: 0.09324, number: 1},
currentChart: {BWP: 1, ZAR: 1.35, USD: 0.01, number: 2}
Run Code Online (Sandbox Code Playgroud)
期望的答案是:
newObject ={BWP: 0, ZAR: 0.05, USD: 0.08324, number: -1}
Run Code Online (Sandbox Code Playgroud)
请不要问我做了什么,因为这是最后一个阶段,因为这是我代码的最后一部分,如果您有兴趣知道我在这里做了什么,那就是:
rates = [
{BWP: 1, ZAR: 1.3, USD: 0.09324, number: 1},
{BWP: 1, ZAR: 1.35, USD: 0.01, number: 2},
{BWP: 1, ZAR: 1.3456, USD: 0.09234, number: 3},
{BWP: 1, ZAR: 1.27894, USD: 0.06788, number: 4}
]
newRate = [];
for(let i in rates){
if( i - 1 === -1 ){ …Run Code Online (Sandbox Code Playgroud) 曾经是这样,当我使用 console.log(obj) 在 Javascript 中显示对象的属性时,属性会按字母顺序显示。最近,这种情况似乎发生了变化。我认为,新订单接近性实际上是如何存储在代码,(虽然我的印象是,对象的属性没有有一个“真实”为了固定在开始阶段)。
然而,对于大型、复杂的类,按字母顺序显示对于查找我正在寻找的属性非常有用。是否可以像以前一样按字母顺序显示属性?
(我还想知道当前属性的“代表”顺序究竟是什么,以及它是如何确定的。)
如何迭代类属性列表并获取每个属性的值(仅属性而不是函数)
class Person{
name:string;
age:number;
address:Address;
getObjectProperties(){
let json = {};
// I need to get the name, age and address in this JSON and return it
// how to do this dynamically, rather than getting one by one
// like json["name"] = this.name;
return json;
}
}Run Code Online (Sandbox Code Playgroud)
请帮忙。
我得到了一个这样的可观察对象:
const original = Observable.of({
a: this.http.get('https://jsonplaceholder.typicode.com/todos/11'),
b: this.http.get('https://jsonplaceholder.typicode.com/todos/22'),
c: this.http.get('https://jsonplaceholder.typicode.com/todos/33')
});
Run Code Online (Sandbox Code Playgroud)
我需要解析内部 observables 并在订阅处理程序中得到类似的东西:
{
a: ResponseFromServer,
b: ResponseFromServer,
c: ResponseFromServer,
}
Run Code Online (Sandbox Code Playgroud)
我应该如何解决这个问题?
谢谢。
编辑:我已经想通了,请阅读下文。
似乎没有多少人知道 *Map 运算符曾经有一个称为resultSelector第二个参数的东西。现在在 rxjs v6 中,你可以用 做同样的事情inner map,让我来告诉你。
const original = Observable.of({
a: this.http.get('https://jsonplaceholder.typicode.com/todos/11'),
b: this.http.get('https://jsonplaceholder.typicode.com/todos/22'),
c: this.http.get('https://jsonplaceholder.typicode.com/todos/33')
});
const transformed = original.pipe(
mergeMap(sourceValue =>
forkJoin(_.values(sourceValue)).pipe(map(resolvedHttpRequests => {
// here you have access to sourceValue as well as resolvedHttpRequests so you can do manual join to match the data.
})) …Run Code Online (Sandbox Code Playgroud) reactive-programming object-properties rxjs reactivex angular
javascript ×7
object ×2
typescript ×2
angular ×1
arrays ×1
console.log ×1
jestjs ×1
powershell ×1
proxy ×1
reactivex ×1
rxjs ×1
undefined ×1