簡體   English   中英

構造函數中的函數返回值

[英]Function return value in constructor

在Javascript中,函數可以是構造函數的參數,但我想在創建新對象時分配要評估的條件值。

var obj = {
  prop: function(){ return 1; },
  prop2: "asd"
};

console.log(obj.prop);  // This prints "function() ..." 

使用這種方法,有什么方法可以使prop值成為1?

對於那些想知道為什么要這樣做的人,我有以下代碼,並且如果某些條件為真或不正確,則需要傳遞不同的offset參數。

$('#primary-menu > ul > li > a[href*=#]').smoothScroll(


{ preventDefault: true, easing: 'swing', speed: 700, offset: -200, exclude: ['.external a'],
    beforeScroll: function () {

        // Disable all waypoints on internal divs which are linked to from the menu
        $('#primary-menu > ul > li > a[href*=#]').each(function () {
            var element_id = MO_THEME.get_internal_link($(this).attr('href')); // Gives me ids of div's with ids like #work,#service, #portfolio etc.
            $(element_id).waypoint('disable');
        });
    },
    afterScroll: function () {
        // Enable all waypoints on internal divs which are linked to from the menu
        $('#primary-menu > ul > li > a[href*=#]').each(function () {
            var element_id = MO_THEME.get_internal_link($(this).attr('href')); // Gives me ids of div's with ids like #work,#service, #portfolio etc.
            $(element_id).waypoint('enable');
        });
    }});

您可以在實例化后更改smoothScroll插件的任何選項:

初始通話后設置選項

如果在調用.smoothScroll()之后需要更改任何選項,則可以通過將“ options”字符串作為第一個參數,將options對象作為第二個參數來進行。

$el.smoothScroll('options', {
  offset: newValue
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM