我正在尝试这个非常简单的例子来自Nicholas Zakas的"专业JavaScript for Web Developers"一书,但是我无法想象我在这里做错了什么.一定是我错过的非常简单的东西,但我被困住了.
这是代码:
'use strict';
var book = {};
Object.defineProperties(book, {
originYear: {
value: 2004,
writable: false
},
_year: {
value: 2004
},
edition: {
value: 1
},
year : {
get: function() {
return this._year;
},
set: function(newValue) {
if(newValue > this.originYear) {
this._year = newValue;
this.edition += newValue - this.originYear;
}
}
}
});
console.log(book.edition);
book.year = 2006;
console.log(book.edition);
Run Code Online (Sandbox Code Playgroud)
我在Chrome控制台上遇到的错误是:
未捕获的TypeError:无法分配给#main.js的只读属性'_year':31 Object.defineProperties.year.setmain.js:39(匿名函数)
有人可以解释我哪里出错了吗?
这是小提琴
当我想从路由器类初始化视图时,我收到此错误.
错误是:未捕获TypeError:对象#没有方法'_ensureElement'
BlogFormView:
App.BlogFormView = Backbone.View.extend({
el: ".data-form",
initialize: function(){
this.template = _.template($("#blog_form_template").html());
this.render();
},
render: function(){
this.$el.html(this.template({blog: this.model.toJSON()}));
return this;
},
events: {
"click .submit-blog" : "submitForm"
},
submitForm: function(ev){
}
});
Run Code Online (Sandbox Code Playgroud)
路由器:
var blog = new App.Blog();
var blogFormView = App.BlogFormView({model: blog});
Run Code Online (Sandbox Code Playgroud) 在这里酶新手。我正在尝试在该组件上调用方法后测试React组件的状态是否正在更新。
这是我正在测试的组件的片段:
class App extends React.Component {
constructor(props) {
super(props);
}
state = {
recipes: {},
user: null
};
handleUnauth = () => {
this.setState({
user: null
});
};
render() {
//render children, pass down methods as props etc...
}
}
Run Code Online (Sandbox Code Playgroud)
下面是测试:
import createRouterContext from 'react-router-test-context';
import { shallow, mount } from 'enzyme';
import expect from 'expect';
import React from 'react';
import App from 'App'; //import with webpack alias
describe('App', () => {
let wrapper, context;
beforeEach(() => {
context …Run Code Online (Sandbox Code Playgroud) 在这段代码中:
if (screen.width <= 800) {
window.location = "http://www.megaoferta.bg/mobile";
}
Run Code Online (Sandbox Code Playgroud)
如何识别使用JavaScript打开页面的URL?
例如,如果用户从Google搜索进入我的网页,我该如何使用JavaScript获取"google.com"页面?