Javascript原型设计forEach on Object

时间:2017-11-19 06:14:19

标签: javascript

我想在对象上创建forEach的原型,这是我的代码

Object.prototype.forEach = function(el,index){
    for(var k in Object){
        el = Object[k];
        index = k;
    }
}

然后我创建了一个对象来测试这个

var obj = {x:0,y:0,z:1290}
obj.forEach( function(el, index) {
    console.log(el+" "+index);
    alert();
});

此代码不返回任何错误,但它既没有提醒也没有登录到控制台。我检查了obj对象,它确实包含了其中的_proto_属性。我也试过了

Object.prototype.forEach = function(el,index){
    for(var k in this){
        el = this[k];
        index = k;
    }
}

2 个答案:

答案 0 :(得分:1)

您实际上希望函数接受一个函数,然后使用每个元素和索引调用该函数:

Object.prototype.forEach = function(f) {
    for (var k in this) {
        el = this[k];
        index = k;
        f(el, index);
    }
}

答案 1 :(得分:0)

所以对象的forEach函数是错误的,你的forEach函数应该是一个函数,你可以参考数组的forEach。

function es_after_setup_theme() {
    /**
     * Ensure customer billing details are not updated if an admin or shop manager is placing an order
     *
     */
    if( es_check_sales_assistant_user() ) {
        add_filter( 'woocommerce_checkout_update_customer_data', '__return_false' );
    }

}

add_action( 'after_setup_theme', 'es_after_setup_theme' );

function es_check_sales_assistant_user() {
    $user = wp_get_current_user();
    $admin_roles = array( 'administrator', 'shop-manager' );

    if( array_intersect( $admin_roles, $user->roles ) ) { 
        return true;
    }

    return false;
}


function es_create_new_customer( $order_id ) {
    if( es_check_sales_assistant_user() ) {
        $order = wc_get_order($order_id);

        // Create a login for the user and assign the new order to them.
        $email_address = $order->get_billing_email();
        $user_id = email_exists( $email_address );

        // Generate password only letters and numbers
        $random_password = wp_generate_password( 8, false );

        if( ! $user_id ) {
            // Create a new WordPress user, WooCommerce function wc_create_new_customer uses a stipped version of the email address for the username.
            $username = sanitize_user( current( explode( '@', $email_address ) ), true );

            // Ensure username is unique.
            $append     = 1;
            $o_username = $username;

            while ( username_exists( $username ) ) {
                $username = $o_username . $append;
                $append++;
            }

            $userdata = array(
                'user_login'    => $username,
                'user_pass'     => $random_password,
                'user_email'    => $email_address,
                'first_name'    => $order->get_billing_first_name(),
                'last_name'     => $order->get_billing_last_name(),
                'role'          => 'customer',
            );

            $user_id = wp_insert_user( $userdata ) ;

            if( $user_id ) {
                // Add WooCommerce billing details
                $customer = new WC_Customer( $user_id );

                $customer->set_billing_first_name( $order->get_billing_first_name() );
                $customer->set_billing_last_name( $order->get_billing_last_name() );
                $customer->set_billing_address_1( $order->get_billing_address_1() );
                $customer->set_billing_city( $order->get_billing_city() );
                $customer->set_billing_postcode( $order->get_billing_postcode() );
                $customer->set_billing_state( $order->get_billing_state() );
                $customer->set_billing_country( $order->get_billing_country() );
                $customer->set_billing_phone( $order->get_billing_phone() );
                $customer->set_billing_email( $email_address );

                $customer->save();
            }
        }

        // Update order to new customer id
        $order->set_customer_id( $user_id );
        $order->save();
    }
}

add_action( 'woocommerce_thankyou', 'es_create_new_customer' );