小编Lar*_*ydx的帖子

未捕获的TypeError:无法分配给只读属性

我正在尝试这个非常简单的例子来自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(匿名函数)

有人可以解释我哪里出错了吗?

这是小提琴

javascript

49
推荐指数
1
解决办法
13万
查看次数

Backbone.js _ensureElement错误

当我想从路由器类初始化视图时,我收到此错误.

错误是:未捕获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)

javascript backbone.js backbone-views backbone-routing

11
推荐指数
1
解决办法
3531
查看次数

在调用更新状态的组件方法后如何测试React状态-使用酶

在这里酶新手。我正在尝试在该组件上调用方法后测试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)

reactjs enzyme

6
推荐指数
1
解决办法
5958
查看次数

获取重定向到此页面的URL

在这段代码中:

if (screen.width <= 800) {
    window.location = "http://www.megaoferta.bg/mobile";
}
Run Code Online (Sandbox Code Playgroud)

如何识别使用JavaScript打开页面的URL?

例如,如果用户从Google搜索进入我的网页,我该如何使用JavaScript获取"google.com"页面?

javascript referrer

0
推荐指数
1
解决办法
79
查看次数