const getMousePosition =(x,y)=>({
x:x,
y:y
});
getMousePosition
是一个简单的函数,它返回一个包含两个字段的对象。 ES6提供了语法糖,以消除必须写入x: x
的冗余。您可以简单地编写一次x
,它将被转换为x: x
(或类似的东西)。这是从上面重写的相同函数使用这个新语法: const getMousePosition =(x,y)=>({x,y});
Person
对象。 {name: "Zodiac Hasbro", age: 56, gender: "male"}
。'
- testString: assert(() => {const res={name:"Zodiac Hasbro",age:56,gender:"male"}; const person=createPerson("Zodiac Hasbro", 56, "male"); return Object.keys(person).every(k => person[k] === res[k]);});
- - text: '不:
被使用了。'
- testString: getUserInput => assert(!getUserInput("index").match(/:/g));
-
-```
-
-componentWillReceiveProps()
,只要组件正在接收新的道具就会调用它。此方法接收新的props作为参数,通常写为nextProps
。您可以使用此参数并与this.props
进行比较,并在组件更新之前执行操作。例如,您可以在处理更新之前在本地调用setState()
。另一种方法是componentDidUpdate()
,并在组件重新渲染后立即调用。请注意,渲染和安装在组件生命周期中被视为不同的东西。首次加载页面时,将挂载所有组件,这是调用componentWillMount()
和componentDidMount()
位置。在此之后,随着状态的改变,组件会重新渲染自己。下一个挑战将更详细地介绍这一点。 Dialog
从其父组件Controller
组件接收message
道具。在Dialog
组件中编写componentWillReceiveProps()
方法,并将this.props
和nextProps
到控制台。您需要将nextProps
作为参数传递给此方法,虽然可以将其命名为任何名称,但nextProps
其命名为nextProps
。接下来,在Dialog
组件中添加componentDidUpdate()
,并记录一条说明组件已更新的语句。此方法的工作方式类似于为您提供的componentWillUpdate()
。现在单击按钮更改消息并观察浏览器控制台。控制台语句的顺序显示调用方法的顺序。 注意:您需要将生命周期方法编写为普通函数而不是箭头函数来传递测试(将生命周期方法编写为箭头函数也没有优势)。 Controller
组件应将Dialog
组件呈现为子组件。
- testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(Controller)); return mockedComponent.find("Controller").length === 1 && mockedComponent.find("Dialog").length === 1; })(), "The Controller
component should render the Dialog
component as a child.");'
- - text: Dialog
组件中的componentWillReceiveProps
方法应将this.props
记录到控制台。
- testString: 'assert((function() { const lifecycleChild = React.createElement(Dialog).type.prototype.componentWillReceiveProps.toString().replace(/ /g,""); return lifecycleChild.includes("console.log") && lifecycleChild.includes("this.props") })(), "The componentWillReceiveProps
method in the Dialog
component should log this.props
to the console.");'
- - text: Dialog
组件中的componentWillReceiveProps
方法应将nextProps
记录到控制台。
- testString: 'assert((function() { const lifecycleChild = React.createElement(Dialog).type.prototype.componentWillReceiveProps.toString().replace(/ /g,""); const nextPropsAsParameterTest = /componentWillReceiveProps(| *?= *?)(\(|)nextProps(\)|)( *?=> *?{| *?{|{)/; const nextPropsInConsoleLogTest = /console\.log\(.*?nextProps\b.*?\)/; return ( lifecycleChild.includes("console.log") && nextPropsInConsoleLogTest.test(lifecycleChild) && nextPropsAsParameterTest.test(lifecycleChild) ); })(), "The componentWillReceiveProps
method in the Dialog
component should log nextProps
to the console.");'
- - text: Dialog
组件应调用componentDidUpdate
方法并将消息记录到控制台。
- testString: 'assert((function() { const lifecycleChild = React.createElement(Dialog).type.prototype.componentDidUpdate.toString().replace(/ /g,""); return lifecycleChild.length !== "undefined" && lifecycleChild.includes("console.log"); })(), "The Dialog
component should call the componentDidUpdate
method and log a message to the console.");'
-
-```
-
-this.remove
。此函数应接受一个值并检查它是否存在于集合中。如果是,请从集合中删除该值,然后返回true。否则,返回false。 Set
类应该有一个remove
方法。
- testString: 'assert((function(){var test = new Set(); return (typeof test.remove === "function")}()), "Your Set
class should have a remove
method.");'
- - text: 您的remove
方法应该只删除集合中存在的项目。
- testString: 'assert.deepEqual((function(){var test = new Set(); test.add("a");test.add("b");test.remove("c"); return test.values(); })(), ["a", "b"], "Your remove
method should only remove items that are present in the set.");'
- - text: 您的remove
方法应从集合中删除给定的项目。
- testString: 'assert((function(){var test = new Set(); test.add("a");test.add("b");test.remove("a"); var vals = test.values(); return (vals[0] === "b" && vals.length === 1)}()), "Your remove
method should remove the given item from the set.");'
-
-```
-
-this.size
,它应返回集合的大小。 Set
类应该有一个size
方法。
- testString: 'assert((function(){var test = new Set(); return (typeof test.size === "function")}()), "Your Set
class should have a size
method.");'
- - text: size
方法应返回集合中的元素数。
- testString: 'assert((function(){var test = new Set(); test.add("a");test.add("b");test.remove("a");return (test.size() === 1)}()), "The size
method should return the number of elements in the collection.");'
-
-```
-
-exponentialGenerator
应该是一个函数。
- testString: 'assert(typeof exponentialGenerator=="function","exponentialGenerator
should be a function.");'
- - text: exponentialGenerator()
应该返回一个数字。
- testString: 'assert(typeof exponentialGenerator(10)=="number","exponentialGenerator()
should return a number.");'
- - text: exponentialGenerator(10)
应该返回144
。
- testString: 'assert.equal(exponentialGenerator(10),144,"exponentialGenerator(10)
should return 144
.");'
- - text: exponentialGenerator(12)
应该返回196
。
- testString: 'assert.equal(exponentialGenerator(12),196,"exponentialGenerator(12)
should return 196
.");'
- - text: exponentialGenerator(14)
应该返回256
。
- testString: 'assert.equal(exponentialGenerator(14),256,"exponentialGenerator(14)
should return 256
.");'
- - text: exponentialGenerator(20)
应该返回484
。
- testString: 'assert.equal(exponentialGenerator(20),484,"exponentialGenerator(20)
should return 484
.");'
- - text: exponentialGenerator(25)
应该返回784
。
- testString: 'assert.equal(exponentialGenerator(25),784,"exponentialGenerator(25)
should return 784
.");'
-
-```
-
-