How do you Set the quot;urlquot; when creating a new Response()?(你如何设置“url?创建新的响应()时?)
问题描述
我正在关注 这个为测试创建模拟响应的示例.
I'm following this example to create a mocked response for a test.
稍作修改,如下图:
 var data = { foo: 'bar'};
 var blob = new Blob([JSON.stringify(data)], {type : 'application/json'});
 var init = { "status" : 200 , "statusText" : "SuperSmashingGreat!" };
 var resp = new Response(blob,init);
console.log(resp.url)
body: (...)
bodyUsed: true
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "SuperSmashingGreat!"
type: "default"
url: ""
这在模拟数据和状态方面做得很好但是,我也想模拟 resp.url 我不知道如何使用构造函数来设置它并且 [因为它是只读的] 我不能单独设置它
this does a good job mocking the data and status however, I also want to mock resp.url I don't see how I can set that using the constructor and [since it's readonly] I can't set it on resp itself
resp.url 
>> ""
resp.url = 'www.test.com'
>> "www.test.com"
resp.url
>> ""
那么如何设置网址呢?
推荐答案
由于 url 实际上是由继承的 getter 定义的,所以可以使用 Object.definedProperty 来定义一个直接在 Response 实例上的简单值属性,它会隐藏继承的 getter 属性:
Since url is actually defined by an inherited getter, you can use Object.definedProperty to define a simple value property directly on your Response instance, which shadows the inherited getter property:
Object.defineProperty(resp, "url", { value: "foobar" });
<小时>
为了自己的理解,可以通过查看Response.prototype.url的属性描述符看到继承的getter:
For your own understanding, you can see the inherited getter by looking at the property descriptor of Response.prototype.url:
Object.getOwnPropertyDescriptor(Object.getPrototypeOf(new Response()), 'url')
> {get: ƒ, set: undefined, enumerable: true, configurable: true}
                        这篇关于你如何设置“url"?创建新的响应()时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:你如何设置“url"?创建新的响应()时?
				
        
 
            
        基础教程推荐
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
 - 如何使用 CSS 显示和隐藏 div? 2022-01-01
 - 什么是不使用 jQuery 的经验技术原因? 2022-01-01
 - Node.js 有没有好的索引/搜索引擎? 2022-01-01
 - 每次设置弹出窗口的焦点 2022-01-01
 - WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
 - 如何在特定日期之前获取消息? 2022-01-01
 - jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
 - 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
 - Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				