我了解到,当使用Object.assign()时,它只扩展顶级对象.我怎样才能深度扩展对象?例如,假设我有以下源对象:
const source = {
id: 1,
otherKey: {},
params: {
page: {
a: 1,
b: {}
},
data: {
b: 1
}
}
}
Run Code Online (Sandbox Code Playgroud)
我这样使用Object.assign():
Object.assign({}, source, {
params: {
page: {
a: 2
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果将是:
{
id: 1,
otherKey: {},
params: {
page: {
a: 2
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何以浅克隆方式保留params.data键和params.page.b键.
oldObject.params.data === newObject.params.data // true
oldObject.params.page === newObject.params.page // false
oldObject.params.page.b === newObject.params.page.b // true
Run Code Online (Sandbox Code Playgroud)
注意:此问题与如何深度合并而不是浅合并不同.那里的答案没有给出预期的结果.
检查此bin从上面的链接中获取答案.
我有一个扩展另一个类的类。例如:
class Store extends BehaviorSubject {
constructor(obj) {
super(obj)
}
}
Run Code Online (Sandbox Code Playgroud)
我还有更多的类 extends Store。我需要的是一种从BehaviorSubject超类中隐藏某些属性的next()方法,例如方法。
class SomeStore extends Store {
}
// I want to hide from this class some methods, properties
// That exists on the BehaviorSubject class.
const s = new SomeStore();
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
我想创建一个向类添加方法的装饰器。
export function Deco() {
return function<T extends { new (...args: any[]): {} }>(constructor: T) {
constructor.prototype.someMethod = function() {
};
};
}
@Deco()
class Test {
}
Run Code Online (Sandbox Code Playgroud)
问题是当我尝试调用添加的方法时,出现打字稿错误:
类型 Test 上不存在属性 someMethod。
const test = new Test();
test.someMethod();
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
我正在阅读这篇文章,其中有一节介绍何时使用markForChange().
在他的示例中,他具有以下组件:
@Component({
selector: 'cart-badge',
template: '{{counter}}',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class CartBadgeComponent {
@Input() addItemStream;
counter = 0;
constructor(private cd: ChangeDetectorRef) {}
ngOnInit() {
this.addItemStream.subscribe(() => {
this.counter++;
this.cd.markForCheck();
});
}
}
Run Code Online (Sandbox Code Playgroud)
他使用该markForCheck()方法来更新视图。我不明白的是,为什么markForCheck()当调用detectChanges() 也更新视图时我们需要在这里使用?
我在 StackOverflow 上阅读了问题的答案 - MarkForCheck() 和 detectorChanges() 有什么区别?
但不符合上面的例子。那么,为什么不打电话detectChanges()呢markForCheck()?
据我了解,useState 钩子的更新函数应该批量运行,而不是每次调用时都重新渲染组件。我创建了以下自定义钩子:
function useObservable(source) {
const [v, setV] = useState();
useEffect(() => {
const subscription = source.subscribe(setV);
return () => subscription.unsubscribe();
}, [source]);
return v;
}
Run Code Online (Sandbox Code Playgroud)
但是当我多次使用它时,每次发射都会导致重新渲染:
const subject = new Subject();
setTimeout(() => {
subject.next("Value");
}, 1000);
function App() {
const value = useObservable(subject);
const value2 = useObservable(subject);
const value3 = useObservable(subject);
console.log("render"); <=== called 3 times
return (
<div>
<p>{value}</p>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法优化这个?我是不是误解了什么?
假设我有一节课:
class Test {
propA;
propB;
propC;
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个返回字符串数组的方法,并将其键入只有Test类中存在的键,我该如何使用该keyof功能?
class Test {
propA;
propB;
propC;
getSomeKeys() : keyof Test[] {
return ['propA', 'propC']
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个名为的文件service.ts,它公开以下代码:
export interface SomeInterface {
keyOne: string;
}
export class TestService<T = SomeInterface> {
property: T;
}
Run Code Online (Sandbox Code Playgroud)
在index.ts文件中,我正在使用该服务:
import { TestService } from './service';
const service = new TestService();
service.property.keyOne
Run Code Online (Sandbox Code Playgroud)
我还创建index.d.ts了SomeInterface用更多键声明相同接口的文件:
export interface SomeInterface {
keyTwo: number;
}
Run Code Online (Sandbox Code Playgroud)
问题是service.property只有“知道”keyOne属性。我怎么能告诉打字稿合并他们两个?
我需要根据搜索词按以下顺序对数组进行排序.
代码:
var arr = ['Something here Hello', 'Hell', 'Hello'];
var term = 'Hello';
var sorted = arr.slice().sort((a, b) => {
let value = 0;
if (a.startsWith(term)) {
value = -1;
}
if (a.indexOf(term) > -1) {
value = -1;
}
if (a === term) {
value = -1;
}
return value;
});
console.log(sorted);Run Code Online (Sandbox Code Playgroud)
预期的结果是:
["Hello", "Hell", "Something here Hello"]
Run Code Online (Sandbox Code Playgroud)
我不确定如何使用内置的排序功能来执行此操作,因为它看起来并不适合与此类情况一起使用.请问有什么建议吗?
今天我了解到,如果例如我有一个对象:
var foo = {a: 1, b: { ... }}
Run Code Online (Sandbox Code Playgroud)
我将它传递给一个函数:
function test(foo) {
foo.b
}
Run Code Online (Sandbox Code Playgroud)
它必须将整个foo对象加载到函数的作用域中才能访问该b属性,这会增加内存消耗。
书中的建议是始终只传递您需要的内容:
function test(b) {
b
}
test(foo.b)
Run Code Online (Sandbox Code Playgroud)
我的问题是这样吗?为什么?对象是通过引用传递的。