function Ninja(){
this.swingSword = function(){
return true;
};
}
// Should return false, but will be overridden
Ninja.prototype.swingSword = function(){
return false;
};
var ninja = new Ninja();
log( ninja.swingSword(), "Calling the instance method, not the prototype method." );
Run Code Online (Sandbox Code Playgroud)
现在日志显示我是真的.这意味着在Ninja.prototype中定义的swingSword已被覆盖,因此如何覆盖构造函数或属性.我知道首选的是构造函数变量然后为什么需要在原型中定义一个函数或属性?
我想在Ember应用程序中的几个视图中包含标准功能.该功能包括将视图的tagName和classNames设置为相同并跟踪每个视图的属性.
问题简而言之:我应该使用mixin还是扩展基本视图?
扩大的问题......
是否应该扩展基本视图来执行此操作?例如:
App.BaseView = Em.View.extend({
tagName: 'section',
classNames: ['page_section', 'blue'],
willInsertElement: function() {
// Some functions called here that set properties
},
});
App.PageOneView = App.BaseView.extend({
// View specific stuff here
});
App.PageTwoView = App.BaseView.extend({
// View specific stuff here
});
Run Code Online (Sandbox Code Playgroud)
...或者,是否应该使用Mixin来扩展功能?例如:
App.BaseMixin = Em.Mixin.create({
tagName: 'section',
classNames: ['page_section', 'blue'],
willInsertElement: function() {
// Some functions called here that set properties
},
});
App.PageOneView = Em.View.extend(App.BaseMixin, {
// View specific stuff here
});
App.PageTwoView = Em.View.extend(App.BaseMixin, {
// …Run Code Online (Sandbox Code Playgroud) 我在尝试运行 JS 条件检查空字符串时遇到了这个错误。在 Chrome 调试器中,空字符串的计算长度为 1,有时甚至为 2。它发生在 React 应用程序中。我对 proto 仍然如何正常工作感到非常困惑,但正常的长度方法却没有。
所以我使用browserify来管理我正在处理的项目的模块.
每个文件都包含以下内容:
module.exports = function(){
// class constructor
}
Run Code Online (Sandbox Code Playgroud)
然后在我的main.js文件中,我创建了这样的新对象:
var Foo = require('./foo.js');
var foo = new Foo();
Run Code Online (Sandbox Code Playgroud)
问题是,我不知道如何在此设置中使用原型继承.通常,如果我希望类Foo从类Bar继承,我会做这样的事情:
Foo.prototype = new Bar();
Foo.prototype.constructor = Foo;
Foo = function(){
Bar.call(this)
}
Run Code Online (Sandbox Code Playgroud)
我将如何使用browserify执行这些操作?我以为我会用module.export替换所有的Foos,但是我试图继承一些来自easeljs的东西而且它只是不起作用.它说'this'没有在容器的初始化器中定义.
提前致谢!
嗨,我有一个日期示例数组,[2019-02-21T04:06:32.000Z]我想将日期转换为所需的格式[02/21/2019 4:06:32 AM].所以我正在使用javascript地图
dateArray.map( x => {
return moment.tz(x, 'Etc/UTC').format('MM/DD/YYYY h:mm:ss A').toString()
});
Run Code Online (Sandbox Code Playgroud)
在此之后我控制台日期数组,但它仍然显示[2019-02-21T04:06:32.000Z]但在map()内,它以所需的格式显示.我在这里做错了什么?任何人都可以帮我解决这个问题吗?谢谢.
javascript ×4
prototype ×3
arrays ×1
browserify ×1
constructor ×1
easeljs ×1
ember.js ×1
mixins ×1
object ×1
overriding ×1
view ×1