我正在创建一个lambda函数,它使用具体的params执行第二个函数.这个代码适用于Firefox,但不适用于Chrome,它的检查器显示一个奇怪的错误,Uncaught TypeError: Illegal invocation
.我的代码出了什么问题?
var make = function(callback,params){
callback(params);
}
make(console.log,'it will be accepted!');
Run Code Online (Sandbox Code Playgroud) 在过去,我看到了下一个CSS,我在想是否存在一些实际的区别
min-width: 90px;
max-width: 90px;
Run Code Online (Sandbox Code Playgroud)
和
width: 90px;
Run Code Online (Sandbox Code Playgroud) 我有一个函数,它试图将参数解析为JSON对象.如果失败,则使用后备.
解析-code.js
function parseCode(code) {
try {
usingJSONFallback(code);
} catch() {
usingStringFallback(code);
}
}
function usingJSONFallback(code) {
JSON.parse(code);
//...more code here
}
function usingStringFallback(code) {
//... more code here
}
Run Code Online (Sandbox Code Playgroud)
main.js
//Some code...
parseCode('hello world!');
Run Code Online (Sandbox Code Playgroud)
我在这段代码中没有看到任何问题.但是,当我尝试为'catch'情况添加一些单元测试(使用Jasmine 2.3)时,Jasmine会自己捕获JSON解析错误并中止测试:
例如,对于Jasmine测试,例如:
describe('parseCode', function() {
it('Parses a string', function() {
var code = 'My code without JSON';
expect(parseCode(code)).toThrow();
});
});
Run Code Online (Sandbox Code Playgroud)
甚至像以下一样的测试:
describe('usingJSONFallback', function() {
it('Throw an error if there is a string', function() {
var code = 'My code without JSON';
expect(usingJSONFallback(code)).toThrow();
});
}); …
Run Code Online (Sandbox Code Playgroud) 我已经创建了下一个获取char*的代码,但是在执行此代码之后,finalResult的大小比预期的大,有一些垃圾字符.为什么??我该如何解决?
//returns void
void processChar(){
//.... more stuff here
// init is a previous char*
char* end = strstr(init,"</div>");
if(end != NULL){
long length = strlen(init) - strlen(end);
if (length > 0){
char* finalResult = malloc(length);
strncat(finalResult, init,length);
//these lengths are different,being strlen(finalResult) > length
NSLog(@"%d %d",strlen(finalResult),length);
//... more stuff here
}
}
return;
}
Run Code Online (Sandbox Code Playgroud) javascript ×2
arrays ×1
c ×1
char ×1
css ×1
invocation ×1
jasmine ×1
json ×1
lambda ×1
memory ×1
objective-c ×1
unit-testing ×1
width ×1