可能创建具有动态名称的新类?

时间:2015-05-19 17:51:21

标签: javascript ecmascript-6

是否可以在js ES6中创建具有不同名称的类?像:

<header>
    <div class="container-fluid full-width-menu navbar-fixed-top">
    <div class="container">
        <div class="row">
                <div class="logo img-responsive col-md-4">
                    <a class="logo-link" href="<?php echo get_bloginfo('url' ); ?>"><img src="<?php bloginfo('stylesheet_directory') ?>/images/logo.svg" /></a>
                </div> <!-- end logo -->    
                <div class=""> <!-- Menu -->
                    <nav class="main-menu navbar-inverse col-md-offset-2 col-md-6" role="banner">
                        <div class="navbar-header">
                          <button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-navbar-collapse">
                            <span class="sr-only">Toggle navigation</span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                          </button>
                        </div>

                            <?php
                                wp_nav_menu( array(
                                    'menu'              => 'primary',
                                    'theme_location'    => 'primary',
                                    'depth'             => 1,
                                    'container'         => 'nav',
                                    'container_class'   => 'navbar-collapse bs-navbar-collapse collapse',
                                    'container_id'      => 'bs-example-navbar-collapse-1',
                                    'menu_class'        => 'nav navbar-nav no-gutter cl-effect-4',
                                    'fallback_cb'       => 'wp_bootstrap_navwalker::fallback',
                                    'walker'            => new wp_bootstrap_navwalker())
                                );
                            ?>
                    </nav>      
                </div> <!-- End menu -->
        </div> <!-- end row -->
    </div> <!-- end container -->
    </div> <!-- end container-fluid -->
    </div>
    </header>

1 个答案:

答案 0 :(得分:0)

可以使用eval

'use strict'
class SomeClass {
    someMethod(){
    }
}
...
var sClassName = "SomeClass";
var variableClass = eval("new " + sClassName+ "()");

variableClass.someMethod();

如果您希望避免使用eval,则可以使用以下内容:

var classByCase = {
   caseA: SomeClass,
   caseB: SomeOtherClass
};
var currentCase = 'caseA';
var variableClass = new classByCase[currentCase]();